Mr. President
Mr. President

Reputation: 1549

Insert a list of integers into a database column

I'm trying to insert a list of integers into a column in a datatable. If I run the following code:

c.executemany("INSERT INTO my_datatable('Column 1') VALUES (?)", list_with_integers)
conn.commit()

I get the following error:

c.executemany("INSERT INTO my_datatable('Column 1') VALUES (?)", list_with_integers)
ValueError: parameters are of unsupported type

I don't understand what I'm doing wrong. I've checked and made sure that all the elements of the list are integers and if I try to run

c.executemany("INSERT INTO my_datatable('Column 1') VALUES (?)", [list_with_integers])

I get:

c.executemany("INSERT INTO my_datatable('Column 1') VALUES (?)", [list_with_integers])
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 559 supplied.

Question: How do I solve this issue and insert a list of integers into a column?

Upvotes: 1

Views: 775

Answers (1)

blhsing
blhsing

Reputation: 106608

The parameter to the executemany method should be a sequence of tuples, so you should convert your list of integers to a sequence of 1-tuples:

c.executemany("INSERT INTO my_datatable('Column 1') VALUES (?)", map(lambda i: (i,), list_with_integers))

Upvotes: 1

Related Questions