Python Bulk Insert gives TypeError: not all arguments converted during string formatting

孤者浪人 提交于 2020-03-26 03:17:10

问题


I have following code to insert multiple rows;

cnx = pymysql.connect(host='xx', user='xx', password='xx', database='xx', autocommit=True)
cursor = cnx.cursor()
query = "INSERT INTO `tableA`('id,'name',...)values(?,? ......)"
cursor.executemany(query, listTosave)

My listTosave contains values like;

[['AS0001', '1170', 1, '1', 'Unknown', 442, 1, datetime.datetime(2018, 5, 28, 23, 0), datetime.datetime(2018, 3, 15, 11, 15), datetime.datetime(2018, 3, 15, 10, 56), datetime.datetime(2018, 5, 28, 23, 18, 26), datetime.datetime(2018, 5, 28, 23, 59, 22), Decimal('15177.3184'), Decimal('15185.7562'), Decimal('8.4378')],[......],...
....]]

When I try to insert I get following error;

    cursor.executemany(query, listTosave)
  File "/tmp/pymodules/pymysql/cursors.py", line 194, in executemany
  File "/tmp/pymodules/pymysql/cursors.py", line 194, in <genexpr>
  File "/tmp/pymodules/pymysql/cursors.py", line 163, in execute
  File "/tmp/pymodules/pymysql/cursors.py", line 142, in mogrify
TypeError: not all arguments converted during string formatting

What am I doing wrong here? I would like to insert multiple rows at once.


回答1:


listTosave should be an list of tuples rather than list of lists

[('AS0001', '1170', 1, '1', 'Unknown', 442, 1, datetime.datetime(2018, 5, 28, 23, 0), datetime.datetime(2018, 3, 15, 11, 15), datetime.datetime(2018, 3, 15, 10, 56), datetime.datetime(2018, 5, 28, 23, 18, 26), datetime.datetime(2018, 5, 28, 23, 59, 22), Decimal('15177.3184'), Decimal('15185.7562'), Decimal('8.4378')),(......),...]


来源:https://stackoverflow.com/questions/50597816/python-bulk-insert-gives-typeerror-not-all-arguments-converted-during-string-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!