gpt4 book ai didi

python - sqlite3.操作错误: unrecognized token: "{"

转载 作者:行者123 更新时间:2023-11-29 07:07:46 25 4
gpt4 key购买 nike

首先,我知道这里已经有类似标题的帖子,我已经查看了这些问题的答案,但仍然无法从这些答案中找出解决方案。 (我的问题与其他问题略有不同)。

我需要能够更新 sqlite 数据库中特定记录的字段值。我用来帮助我解决这个问题的帖子之一是 this .

我在此处的代码中使用了相同的格式:

updateCustomerTable = c.execute("UPDATE customerTable SET {} = ? WHERE customerID = ?").format(age), (newAge.get(),) , (customerID.get(),)

但我收到一条错误消息:

sqlite3.OperationalError: unrecognized token: "{"

当开发人员在其他帖子的答案中使用大括号时,为什么不允许使用大括号?

最佳答案

此处(代码已重新格式化以提高可读性):

c.execute(
"UPDATE customerTable SET {} = ? WHERE customerID = ?"
).format(age)

所以实际上您是在 c.execute() 的结果上调用 .format(),而不是在 SQL 查询字符串上。如果查询是有效的 SQL FWIW,它实际上会引发 AttributeError

你想要的是:

sql = "UPDATE customerTable SET {} = ? WHERE customerID = ?".format(age)  
c.execute(sql, ...)

关于python - sqlite3.操作错误: unrecognized token: "{",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41015330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com