【问题标题】:How to update sqlite3 record in python using WHERE?如何使用 WHERE 在 python 中更新 sqlite3 记录?
【发布时间】:2021-02-15 12:49:16
【问题描述】:

我正在努力用 python 更新我的 sqlite3 数据库中的记录。 我已尝试遵循此tutorial 并根据我的需要从教程中修改此功能:

def update_task(conn, task):
    """
    update priority, begin_date, and end date of a task
    :param conn:
    :param task:
    :return: project id
    """
    sql = ''' UPDATE tasks
              SET priority = ? ,
                  begin_date = ? ,
                  end_date = ?
              WHERE id = ?'''
    cur = conn.cursor()
    cur.execute(sql, task)
    conn.commit()

我有一张这样的桌子:

sql_create_answers_table = """CREATE TABLE IF NOT EXISTS answers (
                                    id integer PRIMARY KEY,
                                    predmet text NOT NULL UNIQUE,
                                    tag text NOT NULL,
                                    keyword text NOT NULL,
                                    obsah text NOT NULL,
                                    autor text NOT NULL,
                                    datum text NOT NULL
                                );"""
db.create_table(sql_create_answers_table)

我的更新函数是这样写的:

def update_answer(answer):
    try:
        conn = create_connection(db_name)
        cur = conn.cursor()
        sql_command = """UPDATE answers 
                        SET predmet = ? ,
                            tag = ? ,
                            keyword = ? ,
                            obsah = ? ,
                            autor = ? ,
                            datum = ? ,
                        WHERE id = ?"""
        # I have also tried this: cur.execute(sql_command, answer )
        cur.execute(sql_command, (answer[1], answer[2], answer[3], answer[4], answer[5], answer[6], answer[0]) )
        conn.commit()                        
        conn.close()
    finally:
        unlock_db()

这就是我建立与数据库的连接的方式:

def create_connection(db_file):
    """ create a database connection to an SQLite database """
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        return conn
    except Error as e:
        print(e)

当我在我的 tkinter 应用程序中调用该函数时,原始答案如下所示:

(1, 'pokus', 'žádný', 'datum', 'Text zpravy a ahoj kamo.\n', 'Vlad', '02/09/21')

然后我创建一个新版本的答案,如下所示:

new_answer = (1, 'pokus', 'žádný', 'datum', 'Text\n', 'Vlad', '02/15/21') # the 4th field changes

然后调用更新函数(db是我导入的模块用的函数):

db.update_answer(new_answer)

我收到一个错误“

File "f:\AAA Vlada\a škola\Python\zapoctak\db_module.py", line 135, in update_answer
    cur.execute(sql_command, (answer[1], answer[2], answer[3], answer[4], answer[5], answer[6], answer[0]) )
sqlite3.OperationalError: near "WHERE": syntax error

"

我的数据库上的其他操作正常,我不明白为什么这个简单的更新功能不起作用。 “更新的答案”在我看来是正确的,更新功能也是如此。

感谢您的帮助,如有需要,我可以提供更多代码。

【问题讨论】:

  • datum = ? , WHERE id -> 去掉,之前的WHERE
  • 感谢您的回答。这是一个相当简单的解决方案,但我找不到它几个小时:)

标签: python database sqlite sql-update


【解决方案1】:

删除WHERE 子句之前多余的, 字符,你应该没问题。

【讨论】:

  • 感谢您的回答。在对教程中的代码进行多次检查时,我真的错过了这一点。祝你有美好的一天。
猜你喜欢
  • 2021-09-06
  • 2021-04-14
  • 2021-06-30
  • 2018-07-21
  • 2012-10-23
  • 1970-01-01
  • 2018-02-17
  • 1970-01-01
相关资源
最近更新 更多