0

I have an int list list = [1, 2, 3]

And a mysql table with an empty second column:

key value
a 
b
c

I want to populate the value column with values from my list so as to make the table look like this:

key value
a   1
b   2
c   3

But I am always getting a TypeError: 'int' object is not iterable

My python code is:

conn = MySQLdb.connect(host="localhost", user="root", passwd="root")
cursor = conn.cursor()
list = [1, 2, 3]
cursor.executemany("INSERT INTO my_table (value) VALUES %d", list)

How am I to solve the issue and get the empty column populated? Thanks!

P.S.: this is just a sample. In reality, the list and the table are much bigger.

0

1 Answer 1

3

You need a list of lists or tuples.

my_list = [[1], [2], [3]]

(Also, don't overwrite the built-in list() type with your own variable.)

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

3 Comments

Thanks! But now I get _mysql_exceptions.ProgrammingError: %d format: a number is required, not str. Changed %d to %s which triggered 1064, "You have an error in your SQL syntax;. Tried the solution from this stackoverflow ticket, but no like either. How am I to solve this?
The placeholder should always be %s.
I have changed the last line to cursor.executemany("INSERT INTO my_table (value) VALUES %s", list), but got another error: 1064, "You have an error in your SQL syntax;. Could you please give me a hint on how to fix that?

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.