且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Python将NULL数据插入MySQL数据库?

更新时间:2022-11-27 12:06:06

快速检查是否为空,如果为空,则将其设置为NULL:

Do a quick check for blank, and if it is, set it equal to NULL:

if(!variable_to_insert)
    variable_to_insert = "NULL"

...然后确保插入的变量不在insert语句的引号中,例如:

...then make sure that the inserted variable is not in quotes for the insert statement, like:

insert = "INSERT INTO table (var) VALUES (%s)" % (variable_to_insert)
...

不喜欢:

insert = "INSERT INTO table (var) VALUES ('%s')" % (variable_to_insert)
...