簡體   English   中英

python MySQL多行插入失敗

[英]python mysql multi-row insert fail

我只是想將數據插入我為進行測試的小型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方法而不是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