python2.7类型错误:不串转换过程中的所有参数格式

问题描述:

#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() 

手工缝制的SQL参数,用executableemany SQL实现,但是脚本的执行将被报告 后的误差如下python2.7类型错误:不串转换过程中的所有参数格式

λ 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参数,但没有发现哪里有问题

+1

您发布显然是断码之前;看看语法高亮。请把它剪下来[mcve] – jonrsharpe

ÿ我们的代码是一个烂摊子,但行

sql = "insert into ti_r_audit values({0},{1},{2},{3})" 

需求,最终被称为沿

sql.format('first', 'second', 'third', 'forth') 

线的东西,从而确保您的列表values包含4个值,当你拨打:

cur.executemany(sql,values) 

否则你会看到错误:

TypeError: not all arguments converted during string formatting 

更新:

对于调试,你可以插入行:

print(sql.format(*values)) 

运行线

cur.executemany(sql,values) 
+0

对不起,代码不完整。我的参数是拼接的一部分,值是参数的一部分,我打印出来,它确实是每行4个参数 – Cooper

+0

检查我的更新并确保所有的SQL语句完好无缺 – AK47

+0

或正在给出,打印出来金额“insert into ti_r_audit values([,(,1 ,,)” – Cooper