Reputation: 9213
I am trying to push a large csv of data into a mysql
database and keep getting errors so I have simplified the insert to the following. I have seen a number of similar questions on here, but I haven't been able to get those to work for me. What am I missing? It should also note that it prints fine when I try that.
import MySQLdb as mdb
con = mdb.connect(host='', port=3306, user='', passwd='', db='')
cursor = con.cursor()
cursor.executemany("INSERT INTO schema.table (Account_Number, Sales_Total) "
"VALUES(%s, %s)", ('Account 1', 2))
error:
TypeError: not enough arguments for format string
Upvotes: 2
Views: 2656
Reputation: 8691
executemany()
should be passed a sequence of tuples as the second argument (see the docs). The following should work for you:
cursor.executemany("INSERT INTO schema.table (Account_Number, Sales_Total) "
"VALUES(%s, %s)", [('Account 1', 2)])
Explanation for the strange error message: In your code you are passing a single tuple, which is also a sequence, so executemany()
tries to format the query with just 'Account 1'
, and therefore complains that it doesn't have enough arguments.
Edit:
P.S. Strings are also sequences (of characters), but specifically string formatting treats them as a single value instead of as a sequence of characters. Otherwise, the original code would have created an error complaining about too many arguments instead of too few...
Upvotes: 3