开发者

Python: MySQLdb: Error: 1064 "You have an error in your SQL syntax."

开发者 https://www.devze.com 2023-02-14 00:17 出处:网络
I\'m new to MySQLdb and Python. I\'m trying to execute the following statement: header_string = \'number_one, number_two, number_three\'

I'm new to MySQLdb and Python. I'm trying to execute the following statement:

header_string = 'number_one, number_two, number_three'
values = '1, 2, 3'
cursor.execute("""INSERT INTO my_table (%s) VALUES (%s)""", (header_string, values))

and it r开发者_StackOverfloweturns with the following error:

Error: 1064 "You have an error in your SQL syntax."

From my limited understanding of MySQLdb the above execute statement should execute the following SQL statement:

INSERT INTO my_table (number_one, number_two, number_three) VALUES (1, 2, 3)

Any ideas what I might be doing wrong?


Try:

header_string = ('number_one','number_two','number_three')
values = (1,2,3)
cursor.execute("""INSERT INTO my_table (%s,%s,%s) VALUES (%s,%s,%s)""", (header_string+values))


Try

cursor.execute("""INSERT INTO my_table (%s) VALUES (%s)""" %  (header_string, values))
0

精彩评论

暂无评论...
验证码 换一张
取 消