PSS
PSS

Reputation: 6101

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 returns 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?

Upvotes: 0

Views: 5483

Answers (1)

MikeyB
MikeyB

Reputation: 3350

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))

Upvotes: 6

Related Questions