Python3 SQLite行不会在Ubuntu上删除,但会在Windows上删除

时间:2018-06-13 19:37:02

标签: python sqlite

我试图删除SQLite3中的一行,在Windows上它会删除该行,但在Ubuntu上它不会被删除。我不确定是什么导致它/如何解决它。两个系统都运行Python 3.6.5,我没有用pip安装SQLite3。

我正在运行以下脚本,该脚本创建了一个db.sqlite,用(键,名称)创建一个用户表并插入一个用户。然后它应该删除它:

import sqlite3

class DBHelper:
    def __init__(self, dbname="db.sqlite"):
        self.dbname = dbname
        self.conn = sqlite3.connect(dbname)
        self.conn.set_trace_callback(print)

    def setup(self):
        stmt = "CREATE TABLE IF NOT EXISTS users (key text PRIMARY KEY, name text)"
        self.conn.execute(stmt)
        stmt = "INSERT INTO users (key, name) VALUES ('Acf233146328cea01fe9648acc3053fa', 125368500)"
        self.conn.execute(stmt)
        self.conn.commit()

    def delete_user(self, name, key):
        stmt = "DELETE FROM users WHERE name = (?) AND key = (?)"
        args = (name, key)
        self.conn.execute(stmt, args)
        self.conn.commit()

    def get_all(self):
        stmt = "SELECT name, key FROM users"
        return [x for x in self.conn.execute(stmt)]


def get_db():
    db = DBHelper()
    return db


name = 125368500
key = 'Acf233146328cea01fe9648acc3053fa'

print('Delete {0} {1}'.format(name, key))
db = get_db()
db.setup()
db.delete_user(name, key)
for user_data in db.get_all():
    print('{0} {1}'.format(user_data[0], user_data[1]))

在Ubuntu上它返回(不正确):

Delete 125368500 Acf233146328cea01fe9648acc3053fa
CREATE TABLE IF NOT EXISTS users (key text PRIMARY KEY, name text)
BEGIN
INSERT INTO users (key, name) VALUES ('Acf233146328cea01fe9648acc3053fa', 125368500)
COMMIT
BEGIN
DELETE FROM users WHERE name = (125368500) AND key = ('Acf233146328cea01fe9648acc3053fa')
COMMIT
SELECT name, key FROM users
125368500 Acf233146328cea01fe9648acc3053fa

在Windows上,它返回(正确):

Delete 125368500 Acf233146328cea01fe9648acc3053fa
CREATE TABLE IF NOT EXISTS users (key text PRIMARY KEY, name text)
BEGIN
INSERT INTO users (key, name) VALUES ('Acf233146328cea01fe9648acc3053fa', 125368500)
COMMIT
BEGIN
DELETE FROM users WHERE name = (125368500) AND key = ('Acf233146328cea01fe9648acc3053fa')
COMMIT
SELECT name, key FROM users

更新:所以我似乎遇到了SQLite 3.11.0中引入的错误:https://sqlite.org/src/info/ef360601 因为Ubuntu 16.04.4 LTS默认带有3.11.0,所以我需要更新版本。

更新2 :通过将名称更改为字符串来更新字典也可以修复它。似乎是sqlite错误报告中的相同案例。

1 个答案:

答案 0 :(得分:0)

因为我使用的是带有SQLite的Ubuntu 16.04,它附带的是版本3.11.0,它有以下错误:https://sqlite.org/src/info/ef360601

要解决这个问题,我需要在创建表/插入时使用正确的类型,然后才能删除行。

固定代码:

import sqlite3

class DBHelper:
    def __init__(self, dbname="db.sqlite"):
        self.dbname = dbname
        self.conn = sqlite3.connect(dbname)
        self.conn.set_trace_callback(print)

    def setup(self):
        stmt = "CREATE TABLE IF NOT EXISTS users (key text PRIMARY KEY, name text)"
        self.conn.execute(stmt)
        stmt = "INSERT INTO users (key, name) VALUES ('Acf233146328cea01fe9648acc3053fa', '125368500')"
        self.conn.execute(stmt)
        self.conn.commit()

    def delete_user(self, name, key):
        stmt = "DELETE FROM users WHERE name = (?) AND key = (?)"
        args = (name, key)
        self.conn.execute(stmt, args)
        self.conn.commit()

    def get_all(self):
        stmt = "SELECT name, key FROM users"
        return [x for x in self.conn.execute(stmt)]


def get_db():
    db = DBHelper()
    return db


name = '125368500'
key = 'Acf233146328cea01fe9648acc3053fa'

print('Delete {0} {1}'.format(name, key))
db = get_db()
db.setup()
db.delete_user(name, key)
for user_data in db.get_all():
    print('{0} {1}'.format(user_data[0], user_data[1]))