pymysql.err.InternalError: (1054, "Unknown column 'None' in 'field list'")
错误提示:
Traceback (most recent call last): File "D:/projectwc/test/dd.py", line 43, in <module> effect_row = cursor.execute("insert into `222` set c={}".format(None)) File "C:\python\lib\site-packages\pymysql\cursors.py", line 166, in execute result = self._query(query) File "C:\python\lib\site-packages\pymysql\cursors.py", line 322, in _query conn.query(q) File "C:\python\lib\site-packages\pymysql\connections.py", line 835, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "C:\python\lib\site-packages\pymysql\connections.py", line 1019, in _read_query_result result.read() File "C:\python\lib\site-packages\pymysql\connections.py", line 1302, in read first_packet = self.connection._read_packet() File "C:\python\lib\site-packages\pymysql\connections.py", line 981, in _read_packet packet.check_error() File "C:\python\lib\site-packages\pymysql\connections.py", line 393, in check_error err.raise_mysql_exception(self._data) File "C:\python\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.InternalError: (1054, "Unknown column 'None' in 'field list'")
import pymysql # 创建连接 conn = pymysql.connect(host='192.168.72.135', port=3306, user='root', passwd='123456', db='test') # 创建游标 cursor = conn.cursor() # 执行SQL,并返回收影响行数 effect_row = cursor.execute("insert into `222` set c={}".format(None)) # 提交,不然无法保存新建或者修改的数据 conn.commit() # 关闭游标 cursor.close() # 关闭连接 conn.close()
解决办法:
把None 换成“Null”
effect_row = cursor.execute("insert into `222` set c={}".format("Null"))
还要注意字符串"Insert into 库.表 (`ID`, ....) values (123, 'Null'), ()" 如果要空值需要在替换一下
sql = sql.replace("'Null'", "Null")
作者:沐禹辰
出处:http://www.cnblogs.com/renfanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
出处:http://www.cnblogs.com/renfanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。