简体   繁体   English

python MySQL多行插入失败

[英]python mysql multi-row insert fail

i'm just trying to insert data into a small mysql (innodb) table i made for testing. 我只是想将数据插入我为进行测试的小型mysql(innodb)表中。 the single row insert works: 单行插入有效:

cursor.execute("insert into testy (one, two) values (%s,%s)", ('00','01'))

but multiple row insert fails: 但是多行插入失败:

cursor.execute("insert into testy (one, two) values (%s,%s)", [('00','01'),('10','11'),('20','21')])

and gives the following error: 并给出以下错误:

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)

i need to do the multi-row insert as it is much quicker than looping over the single row inserts. 我需要执行多行插入,因为它比循环单行插入要快得多。 what am i doing wrong? 我究竟做错了什么?

Use executemany method instead of execute : 使用executemany方法而不是execute

cursor.executemany("insert into testy (one, two) values (%s,%s)",
                   [('00','01'),('10','11'),('20','21')])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM