python2.7 TypeError:在字符串格式化过程中并非所有参数都已转换
手动缝合了sql的参数,用ExecutableMany实现sql,但实现后会报告脚本 错误如下python2.7 TypeError:在字符串格式化过程中并非所有参数都已转换,python,mysql,python-2.7,python-2.x,Python,Mysql,Python 2.7,Python 2.x,手动缝合了sql的参数,用ExecutableMany实现sql,但实现后会报告脚本 错误如下 #endcding=utf-8 import MySQLdb conn = MySQLdb.connect{ host='localhost' port=3306, user='root', passwd='admin db='db01', charset='utf8 ) cur = conn.cursor(
#endcding=utf-8
import MySQLdb
conn = MySQLdb.connect{
host='localhost'
port=3306,
user='root',
passwd='admin
db='db01',
charset='utf8
)
cur = conn.cursor()
count = cur.execute("select * from t_r_def_audit")
results = cur.fetchmany(count)
provcode = cur.execute("select * from t_r_params where
param_tag='PROVINCE_CODE' and param_code not in(1,95,99)")
provResults = cur.fetchmany(provcode)
i = 0
sql = "insert into ti_r_audit values({0},{1},{2},{3})"
values = "["
for result in results:
i = i+1
prev = result[8]
audit_id = result[0]
if(prev == "1"):
prov = 31
for index in range(prov):
values =values + "("+str(i)+","+audit_id+",'"+provResults[index]
[0]+"',0),"
i = i+1
elif prev=="0":
values =values + "("+str(i)+","+audit_id+",'"+provResults[31][0]+"',0),"
values = values + "]"
cur.executemany(sql,values)
cur.close
conn.commit()
conn.close()
λpython audit.py
回溯(最近一次呼叫最后一次):
文件“audit.py”,第34行,在
当前执行代码(sql,值)
文件“C:\Python27\lib\site packages\MySQLdb\cursors.py”,第255行,在
执行官
errorhandler(self,TypeError,msg)
文件“C:\Python27\lib\site packages\MySQLdb\connections.py”,第36行,在
defaulterrorhandler
提高errorclass,errorvalue
TypeError:在字符串格式化过程中并非所有参数都已转换
自己写一个手动参数,没有发现错误,说明脚本应该没有问题,问题可能是在拼接sql参数,但是没有找到哪里有问题你的代码很乱,但是行
λ python audit.py
Traceback (most recent call last):
File "audit.py", line 34, in <module>
cur.executemany(sql,values)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 255, in
executemany
self.errorhandler(self, TypeError, msg)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in
defaulterrorhandler
raise errorclass, errorvalue
TypeError: not all arguments converted during string formatting
最终需要被称为
sql = "insert into ti_r_audit values({0},{1},{2},{3})"
因此,在调用时,请确保您的列表值包含4个值:
sql.format('first', 'second', 'third', 'forth')
否则,您将看到错误:
cur.executemany(sql,values)
更新:
要进行调试,可以插入以下行:
TypeError: not all arguments converted during string formatting
在你开始排队之前
print(sql.format(*values))
你发布的代码显然已被破坏;查看语法突出显示。请把它简化为一个对不起,代码不完整。我的参数是拼接值的一部分是参数的一部分,我打印出来,实际上每行有四个参数检查我的更新并确保所有SQL语句都是给定的,打印出金额“插入到ti_r_审计值([,(,1,,)”
cur.executemany(sql,values)