Reputation: 65
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?
Upvotes: 3
Views: 6614
Reputation: 169434
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,))
Upvotes: 6