![](/img/trans.png)
[英]TypeError: not all arguments converted during string formatting - sqlite
[英]SQLite and Python, TypeError: not all arguments converted during string formatting
我正在使用Python将csv行插入SQLite数据库的一些代码。 我已经使用了一段时间,并且一直在工作。 但是,对CSV文件进行了更新,但仅添加了行,而不添加了列。 现在,当我运行代码时,我收到“ TypeError:不是在格式化字符串时转换了所有参数”。 我已经尝试了所有可以想到和发现的内容,但仍然出现此错误。 这是执行数据库插入部分的代码
fin = open("file.csv", "rb")
creader = csv.reader(fin, delimiter=',')
sql_insert = "INSERT INTO WR_Training VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
# Iterate through creader and insert the values into the table
# Pop off the 3 resources columns and insert the others
for row in creader:
row.pop(27)
row.pop(26)
row.pop(25)
if row[2] != "":
cu.execute(sql_insert, row)
fin.close()
cx.commit()
cx.close()
最初,一共有29列,这就是为什么我使用row.pop弹出不需要的列的原因,因此,我也没有在表的原始创建中也包括它们。 因此它存储了29列中的26列。我以前使用%s而不是? 对于sql命令,但是%s无效,现在呢? 效果不佳。
任何建议将非常有帮助!
谢谢
for row in creader:
row = row[0:29]
row.pop(27)
row.pop(26)
row.pop(25)
if row[2] != "":
cu.execute(sql_insert, row)
由于XTL的评论,此方法有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.