我只是想将数据插入到我为测试而创建的小型 mysql (innodb) 表中。单行插入有效:
cursor.execute("insert into testy (one, two) values (%s,%s)", ('00','01'))
但是多行插入失败:
cursor.execute("insert into testy (one, two) values (%s,%s)", [('00','01'),('10','11'),('20','21')])
并给出以下错误:
TypeError - not all arguments converted during string formatting
traceback:
File "./testy.py", line 20, in <module>
cursor.execute("insert into testy (one, two) values (%s,%s)", [('00','01'),('10','11'),('20','21')])
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 159, in execute
query = query % db.literal(args)
我需要进行多行插入,因为它比循环单行插入要快得多。我做错了什么?
最佳答案
使用executemany
method而不是 execute
:
cursor.executemany("insert into testy (one, two) values (%s,%s)",
[('00','01'),('10','11'),('20','21')])
关于python mysql多行插入失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20297819/