Im trying to save bunch of tuples in DB
cursor = cnx.cursor()
query = \"\"\"INSERT INTO `TableA`
(`clientid`,
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
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