Python3 SQLite row won't be removed on Ubuntu, but will be removed on Windows

可紊 提交于 2021-02-08 09:26:05

问题


I'm trying to remove a row in SQLite3, on windows it removes the row, but on Ubuntu it won't. I'm not sure what's causing it/how to fix it. Both systems are running Python 3.6.5, and I did not install SQLite3 with pip.

I'm running the following script, which creates a db.sqlite, creates a user table with (key, name) and inserts one user. Then it should remove it:

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]))

On Ubuntu it returns (incorrect):

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

On Windows it returns (correct):

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

Update: So it seems like I'm experiencing a bug introduced in SQLite 3.11.0: https://sqlite.org/src/info/ef360601 And because Ubuntu 16.04.4 LTS comes with 3.11.0 by default I am going to need to update the version.

Update 2: Updating the typings by changing name to string fixes it as well. Seems to be the same case in the sqlite bug report.


回答1:


Because I was using Ubuntu 16.04 with SQLite it ships with version 3.11.0, which has the following bug: https://sqlite.org/src/info/ef360601

To solve this, I need to use correct types when creating the table/inserting before I can delete the rows.

The fixed code:

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]))


来源:https://stackoverflow.com/questions/50844971/python3-sqlite-row-wont-be-removed-on-ubuntu-but-will-be-removed-on-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!