简体   繁体   中英

Python MySQL insert NULL (None) value error

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM