我有一个数据清单,例如
infoarray[['1.', 'Name1', 'details1, '...', '...', '....'], ['2.', 'Name2, 'details2', '...', '...', '...'], ['3.', 'Name3', 'details3', '...', '...', '...']...]
我只是想将前3个条目添加到具有以下格式的数据库表中
[PLACE],[NAME],[DETAILS]
应该比较简单。数据已经排序,我只需要将每个内部数组的前3个元素附加到数据库中即可。我尝试了以下代码,但出现错误。
//using pymysql
cur = conn.cursor()
cur.executemany("""
INSERT INTO
myTable
(place, name, details)
VALUES
(%s, %s, %s)
""", infoarray)
db.commit()
cur.close()
conn.close()
错误是“ TypeError:不是在格式化字符串时转换了所有参数”,这是假定的意思是我的格式错误。我是python的新手,所以我对使用pymysql的细微差别非常熟悉。
发生这种情况是因为您正在格式化查询中的3个值,但每个arra传递超过3个元素。
尝试将executemany
呼叫更改为:
cur.executemany("""
INSERT INTO
myTable
(place, name, details)
VALUES
(%s, %s, %s)
""", [a[:3] for a in infoarray])
这样,您将只获得每个数组项中的前3个元素,并将其传递给 executemany
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句