Reputation: 9692
Im trying to save bunch of tuples in DB
cursor = cnx.cursor()
query = """INSERT INTO `TableA`
(`clientid`,
`createddatetime`,
`siteid`,...)
VALUES(?,?,?,...)"""
cursor.executemany(query, listTosave)
My listTosave contains list of tuples 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'), Decimal('1313.0547'), Decimal('1313.6179'), Decimal('0.5632'), Decimal('0.0000'), Decimal('0.0000'), Decimal('0.0000'), Decimal('0.0000'), Decimal('0.0000'), Decimal('0.0000'), Decimal('24.6518'), Decimal('24.6518'), 15101.7062, 0.0, 0.0, 0.0, 24.6563), (........... )]
When I try to save I get;
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
Why do I get this error?
Edit : I converted datetime objects/decimal objects also to string. My new list is like;
[('AS0001', '1170', '1', '1', 'Unknown', '442', '1', '2018-05-28 23:00:00', '2018-03-15 11:15:00', '2018-03-15 10:56:00', '2018-05-28 23:18:26', '2018-05-28 23:59:22', '15177.3184', '15185.7562', '8.4378', '1313.0547', '1313.6179', '0.5632', '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '24.6518', '24.6518', '15101.7062', '0.0', '0.0', '0.0', '24.6563'), (.....)]
But still I get same error
Upvotes: 1
Views: 311
Reputation: 1624
Here is a minimal example that I got working.
query = "INSERT INTO `pet`(`name`,`owner`) values(%s,%s)"
listTosave = [('some','shsjhs'),('sosos','shshsh')]
cursor.executemany(query, listTosave)
Make sure that you have a list of tuples, and that you use %s
in query string
Upvotes: 1
Reputation: 4460
At a guess from the error that you're returning it could be to do with the datetime objects that are stored in your list, not converting to a string representation correctly. Wrapping these in str()
might be the cause of your issue.
Note the following example:
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2018, 5, 31, 8, 42, 48, 172575)
>>> str(datetime.datetime.now())
'2018-05-31 08:42:53.192586'
Another option might be able to get rid your error by using json.dumps the list element to a json
string format. But it depends on how you want to store your data in your database. See the following:
>>> import json
>>> element = (
... 'AS0001', '1170', '1', '1', 'Unknown', '442', '1', '2018-05-28 23:00:00', '2018-03-15 11:15:00', '2018-03-15 10:56:00',
... '2018-05-28 23:18:26', '2018-05-28 23:59:22', '15177.3184', '15185.7562', '8.4378', '1313.0547', '1313.6179', '0.5632',
... '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '24.6518', '24.6518', '15101.7062', '0.0', '0.0', '0.0',
... '24.6563')
>>> json.dumps(element)
'["AS0001", "1170", "1", "1", "Unknown", "442", "1", "2018-05-28 23:00:00", "2018-03-15 11:15:00", "2018-03-15 10:56:00", "2018-05-28 23:18:26", "2018-05-28 23:59:22", "15177.3184", "15185.7562", "8.4378", "1313.0547", "1313.6179", "0.5632", "0.0000", "0.0000", "0.0000", "0.0000", "0.0000", "0.0000", "24.6518", "24.6518", "15101.7062", "0.0", "0.0", "0.0", "24.6563"]'
When retrieving this data you could then use json.loads to load it back into a python object format
Upvotes: 1