Cruz Reyes Cruz Reyes - 9 months ago 46
Python Question

How to add to a database from a list?

I'm trying to create a column in SQLITE from a list of integers but get the following error:

"sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 4 supplied."

Here's my code:

conn = sq.connect('test.db')
c = conn.cursor()

def create_table():
c.execute('CREATE TABLE IF NOT EXISTS table(listItem VALUE)')

def data_entry():
x = 0
list1 = [61,33,4,5]
n = [l for l in list1]
while x < len(list1):
listItem = n
c.execute("INSERT INTO tell(listItem) VALUES (?)",
x += 1


Thanks in advance! :)

Answer Source

If I understand correctly, you want to loop the list, not insert the entire list in one row

for l in list1:
    c.execute("INSERT INTO tell(listItem) VALUES (?)",
      (l, ))

And your CREATE TABLE statement should be creating tell, since table is a reserved word and that's what you're inserting to