Dan
Dan

Reputation: 51

Python: Can't insert list values into a mysql column

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.

Upvotes: 0

Views: 325

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599470

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

Upvotes: 3

Related Questions