gpt4 book ai didi

python - MySQL #1054 UPDATE 查询错误代码

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

我目前有一个 MySQL 表,如下所示:

mysql> SELECT * FROM settings;
+--------------+-------+
| name | value |
+--------------+-------+
| connected | 0 |
+--------------+-------+

我正在尝试使用以下代码通过 Python 连接的时间戳来更新该值:

db1 = MySQLdb.connect(host="localhost", user="user", passwd="password", db="database")
cursor1 = db1.cursor()
cursor1.execute("UPDATE settings SET value = 'ts' WHERE name = connected")
db1.commit()
cursor1.close()

这返回给我:

cursor1.execute("""UPDATE settings SET value = 'ts' WHERE name = connected""")
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue _mysql_exceptions.OperationalError: (1054, "Unknown column 'connected' in 'where clause'")

我就是想不通。任何帮助将不胜感激。

最佳答案

1054, "Unknown column 'connected' in 'where clause'

此错误清楚地表明 connected 被视为列名称。

但是这里connected是要与列名name匹配的字符串值。

因此,您需要在 connected 字符串周围放置单引号来解决问题:

cursor1.execute("UPDATE settings SET value = 'ts' WHERE name = 'connected'")

关于python - MySQL #1054 UPDATE 查询错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38459126/

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