Python: MySQLdb: Error: 1064 "You have an error in your SQL syntax."
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))
精彩评论