Can';t在mysql int字段中插入None

Can';t在mysql int字段中插入None,mysql,pymysql,Mysql,Pymysql,我需要在MySql int字段中插入一个int或None(可以为null)。 这是我的代码: cur = db.cursor() contents = [None, 3] for element in contents: print(element) cur.execute('insert into test.new_test (blubb) values (%s)', element) db.commit() 当我不使用None,而是使用另一个int时,这是有效的。我只是不明

我需要在MySql int字段中插入一个int或None(可以为null)。 这是我的代码:

cur = db.cursor()
contents = [None, 3]
for element in contents:
    print(element)
    cur.execute('insert into test.new_test (blubb) values (%s)', element)
db.commit()
当我不使用None,而是使用另一个int时,这是有效的。我只是不明白,为什么没有一个不起作用,我得到了错误

pymysql.err.ProgrammingError:(1064,“您的SQL中有一个错误 语法;检查与MySQL服务器版本对应的手册 对于要在第1行“%s”附近使用的正确语法

尽管我为此找到了所有解决方案,但%s无法处理任何问题…

另外,是否有一种方法可以在不使用for循环的情况下一次添加整个列表(每个条目一个新行)?

在mysql中,它应该为空,因此您需要将其更改为:

cur = db.cursor()
contents = ['NULL', 3]
for element in contents:
    print(element)
    cur.execute('insert into test.new_test (blubb) values (%s)', element)
db.commit()
cur = db.cursor()
contents = [None, 3]
for element in contents:
    print(element)
    if element==None:
        cur.execute('insert into test.new_test (blubb) values (NULL)')
    else:
        cur.execute('insert into test.new_test (blubb) values (%s)', element)
db.commit()
编辑

要解决新的isue,您可以将其更改为:

cur = db.cursor()
contents = ['NULL', 3]
for element in contents:
    print(element)
    cur.execute('insert into test.new_test (blubb) values (%s)', element)
db.commit()
cur = db.cursor()
contents = [None, 3]
for element in contents:
    print(element)
    if element==None:
        cur.execute('insert into test.new_test (blubb) values (NULL)')
    else:
        cur.execute('insert into test.new_test (blubb) values (%s)', element)
db.commit()

另一种方法是更改strict模式,就像mysql中解释的那样,它应该为NULL,因此您需要将其更改为:

cur = db.cursor()
contents = ['NULL', 3]
for element in contents:
    print(element)
    cur.execute('insert into test.new_test (blubb) values (%s)', element)
db.commit()
cur = db.cursor()
contents = [None, 3]
for element in contents:
    print(element)
    if element==None:
        cur.execute('insert into test.new_test (blubb) values (NULL)')
    else:
        cur.execute('insert into test.new_test (blubb) values (%s)', element)
db.commit()
编辑

要解决新的isue,您可以将其更改为:

cur = db.cursor()
contents = ['NULL', 3]
for element in contents:
    print(element)
    cur.execute('insert into test.new_test (blubb) values (%s)', element)
db.commit()
cur = db.cursor()
contents = [None, 3]
for element in contents:
    print(element)
    if element==None:
        cur.execute('insert into test.new_test (blubb) values (NULL)')
    else:
        cur.execute('insert into test.new_test (blubb) values (%s)', element)
db.commit()

另一种方法是改变严格模式,就像它解释的那样。请用NULL替换NONE。传递到MySQL服务器的命令应如下所示:

insert into test.new_test (blubb) values (NULL)
其中blubb是表new_test中的有效列,且必须允许空值


干杯,

请将无替换为空。传递到MySQL服务器的命令应如下所示:

insert into test.new_test (blubb) values (NULL)
其中blubb是表new_test中的有效列,且必须允许空值


干杯,

如果我这样做,我会得到
pymysql.err.InternalError:(1366,“第1行'blubb'列的整数值不正确:'NULL”)
Contents=['',3]?pymysql.err.InternalError:(1366,“第1行'blubb'列的整数值不正确:'')如果我这样做,我会得到
pymysql.err.InternalError:(1366,“第1行“blubb”列的整数值不正确:'NULL'”
Contents=['',3]?pymysql.err.InternalError:(1366,“第1行“blubb”列的整数值不正确:'')我该怎么做?如果我试图用“NULL”或“”,我会得到一个错误…(请参阅其他答案)我该怎么做呢?如果我尝试将None替换为Null或“”,我会得到一个错误…(请参阅其他答案)