python笔记--(3)--[向MySQL数据库中插入null值]

参考博客:菜鸟的成长的博客
python中空值为None,而MySQL中的空值为NULL,在插入空值时可通过如下方法完成:

import pymysql

conn = pymysql.connect(host="localhost", user="root", passwd="12345", db="MyDB", charset="utf-8")

cur = conn.cursor()

cur.execute("**insert into** My_table(attr1, attr2, attr3) **values** (%s, %s, %s)",
                (entry_1 **or** "NULL" , entry_2 **or** "NULL", entry_3 **or** "NULL"))

cur.close()

conn.commit()

conn.close()

你可能感兴趣的:(python笔记--(3)--[向MySQL数据库中插入null值])