execute 可以自动将python的None转化为MySQL的NULL
import pymysql a = '' b = 'NULL' c = None def updata(): conn = pymysql.connect(host="localhost", port=3306, user="root", password="123", db="xctest") cursor = conn.cursor() sql="insert into kk(name,explains) VALUES (%s, %s)" # cursor.execute(sql, (a, "插入空字符串")) # cursor.execute(sql, (b, "手工转化NULL")) cursor.execute(sql, (c, "插入空置None")) conn.commit() print("haha") updata()