I am trying to insert a NULL value into a MySQL db int field with a python script. NOT NULL is off on the field so its not that. I have manually inserted a NULL value into database and that worked fine and my code works fine if I put a literal value in the place of None.
I have looked a several people's examples of how to do this and as far as I can tell there is nothing wrong in syntax.
Code:
length2 = None
cursor.execute("INSERT INTO tableName(Length) VALUES(%s)", length2)
Error:
Error 1064: You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'%s)' at line 1
Any ideas?
Aaron W.
9,2952 gold badges36 silver badges47 bronze badges
1 Answer 1
The second argument to execute() should be a tuple (or list).
Please change your code to:
cursor.execute("INSERT INTO tableName (Length) VALUES (%s);", (length2,))
answered May 24, 2012 at 20:54
mechanical_meat
171k25 gold badges238 silver badges231 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
execute()should be a tuple (or list). Please change tocursor.execute("INSERT INTO tableName(Length) VALUES(%s)", (length2,))and report back.[length2]is a list without commas.