急急急 python插入不了中文 直接在sql语句中写中文可以插入 变量变成中文就插入不了了
a = cur.executemany(sql_insert.encode('utf8'),(names,times,size,username))
b = cur.rowcount
db.commit()
报错
names=10.5.13.66上的aushelp2(Z).lnk
Traceback (most recent call last):
File "E:\Symantec\monitoring_user_file.py", line 55, in <module>
a = cur.execute(sql_insert.encode('utf-8'),(names,times,size,username))
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 202, in execute
self.errorhandler(self, exc, value)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (1366, "Incorrect string value: '\\xC9\\xCF\\xB5\\xC4au...' for column 'file_name' at row 1")
a = cur.executemany(sql_insert.encode('utf8'),('中文测试',times,size,username))
b = cur.rowcount
db.commit()
没问题
为什么变量是中文插入不行而直接给中文就可以呢
[解决办法]
cur.executemany(sql_insert,(names.encode('utf-8'),times,size,username))