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

 

posted on 2019-07-14 12:56  天马行宇  阅读(1218)  评论(0编辑  收藏  举报