Reputation: 179
Here is my python code:
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()
and when I run it, the error infomation is:Traceback (most recent call last):
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 version is 5.6.38. I don't get the right answer from google, please help. thanks.
Upvotes: 0
Views: 1519
Reputation: 1368
As you can see in the error message, the column "l" is unknown. It's because of your loop. I don't think you really have a column name "l" but a column name "liaogx". If so, you have to change your loop to this :
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)
Upvotes: 1