gpt4 book ai didi

python - mysql_exceptions.OperationalError : (1054, "Unknown column ' xxx'在 'field list'“)

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

这是我的Python代码:

import MySQLdb

db = MySQLdb.connect("127.0.0.1", "root", "liao", "adminset")
cursor = db.cursor()

for i in "liaogx":
sql = """INSERT INTO cmdb_hostgroup(name, created_at, updated_at) VALUES (%s, "2018-01-31 07:50:26.879000", "2018-01-31 07:50:26.879000");""" % (i, )
cursor.execute(sql)

db.close()

当我运行它时,错误信息是:Traceback(最近一次调用最后):

File "/root/Projects/adminset-master/cmdb/test.py", line 124, in <module>
cursor.execute(sql)
File "/root/.virtualenvs/adminset/lib/python2.7/site-packages/MySQLdb/cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "/root/.virtualenvs/adminset/lib/python2.7/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
_mysql_exceptions.OperationalError: (1054, "Unknown column 'l' in 'field list'")

MySQL 版本是 5.6.38。我没有从谷歌得到正确的答案,请帮忙。谢谢。

最佳答案

正如您在错误消息中看到的,“l”列未知。这是因为你的循环。我不认为你真的有一个列名“l”,而是一个列名“liaogx”。如果是这样,您必须将循环更改为:

for i in ["liaogx"]:  # I just add brackets
sql = """INSERT INTO cmdb_hostgroup(name, created_at, updated_at) VALUES (%s, "2018-01-31 07:50:26.879000", "2018-01-31 07:50:26.879000");""" % (i, )
cursor.execute(sql)

关于python - mysql_exceptions.OperationalError : (1054, "Unknown column ' xxx'在 'field list'“),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48543445/

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