这是我的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()


而当我运行它时,错误信息是:跟踪(最近一次呼叫最近):

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。我没有从Google那里得到正确的答案,请帮忙。谢谢。

最佳答案

正如您在错误消息中看到的那样,列“ 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,“字段列表”中的未知列“xxx”),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48543445/

10-12 12:55
查看更多