2

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
1

1 Answer 1

4

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.