关于pymysql报错TypeError %d format a number is required,not str问题
现场环境:
python 3.8.1 ,pymysql 0.9.3
放报错代码
import pymysql
def getCollectToMysql():
# 打开数据库连接
dbConnect = pymysql.connect(host='localhost',user='root',passwd='root',db='issuehandledb',port=3306,charset='utf8')
# 使用cursor()方法获取操作游标
cursor = dbConnect.cursor()
return cursor, dbConnect
#使用列表内元组的方式增加
def insertDataToMysql(cursor, dbConnect, insertData, sql):
try:
cursor.execute(sql, insertData)
dbConnect.commit()
except:
# Rollback in case there is any error
# 输出异常信息
print(insertData)
traceback.print_exc()
dbConnect.rollback()
if __name__ == '__main__':
cursor,dbConnect = getCollectToMysql()
insertsql = 'insert into issuehandledb.userinfo(id,username,password,privilege,isused) values(%d,%s,%s,%d,%s);'
result = (8,'xccc','sss',4,'1')
insertDataToMysql(cursor,dbConnect,result,insertsql)
cursor.close()
dbConnect.close()
报错如下:
解决方案:(网上查到各种都是把%d改成%s,本小白偏要用%d)
根据报错,打入各种断点后发现执行到如下文件中报错,强行把int类型数据转为了str类型
converters.py文件中
def escape_int(value, mapping=None):
return str(value)
修改为正常无报错
def escape_int(value, mapping=None):
#return str(value)
return int(value)
总结
有点坑,查了各种百度,有类似现象,但是没具体解决方案,故此为记录