gpt4 book ai didi

python2.7类型错误: not all arguments converted during string formatting

转载 作者:行者123 更新时间:2023-11-28 23:11:03 27 4
gpt4 key购买 nike

#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,但是脚本执行后会报错错误如下

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

最佳答案

你的代码很乱,但是行

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)

关于python2.7类型错误: not all arguments converted during string formatting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45960973/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com