Python MySQL inserts a NULL (None) error

I am trying to insert a NULL value in a MySQL db int field using a python script. NOT NULL is off on the field, so it is not. I manually entered a NULL value in the database, and it worked fine, and my code works fine if I put a literal value instead of None.

I looked at a few examples of how to do this, and as far as I can tell, there is nothing wrong with the syntax.

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

+3
source share
1 answer

The second argument execute()must be a tuple (or list).
Please change your code to:

cursor.execute("INSERT INTO tableName (Length) VALUES (%s);", (length2,))
+4
source