I followed the recommendations made here and here to insert records into a MySQL table, but the "not all arguments converted during string formatting" error persists.
import csv
import MySQLdb
//snip// # MySQL server login code
MyCSV = open('file.csv')
csv_data = csv.reader(MyCSV)
records = []
for row in csv_data:
if "Request_Date" in row:
# skip header row (field 1 header = 'Request_Date')
continue
else:
records.append(tuple(row))
cursor.executemany('INSERT INTO SchemaName.TableName VALUES
(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', records)
MyDB.commit()
cursor.close()
MyDB.close()
Each tuple in 'records' contains 15 elements, some of which are blank.
print(records[:2]) generates...
[('MM/DD/YYYY', 'first last', 'a-b/c', 'abcde-f', '', '', 'No', '', '', '', '', '', '', 'category-a', ''), ('MM/DD/YYYY', 'first last', 'e-f/g', 'abcde-f', '', '', 'No', '', '', '', '', '', '', 'category-b', '')]
Here is the error that results when the 'cursor.executemany' line is run:
File "C:\Users\mburnett\AppData\Local\Programs\Python\Python36-32\lib\site-packages\MySQLdb\cursors.py", line 238, in execute
query = query % args TypeError: not all arguments converted during string formatting
During handling of the above exception, another exception occurred:
<<<...>>>
File "C:\Users\mburnett\AppData\Local\Programs\Python\Python36-32\lib\site-packages\MySQLdb\connections.py", line 52, in defaulterrorhandler
raise errorclass(errorvalue)
_mysql_exceptions.ProgrammingError: not all arguments converted during string formatting
I noticed in Kashyap's answer (second link) that his list of tuples ('purchases') has a comma before the last bracket. My list of tuples does not have this trailing comma. If that last comma is necessary for this to work, what's the best method of getting it there?
Thanks in advance!
running Python 3.6.4/Windows 10