Cruz Reyes Cruz Reyes - 20 days ago 7
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 (?)",
(listItem))
x += 1
conn.commit()


create_table()
data_entry()
c.close()
conn.close()


Thanks in advance! :)

Answer

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, ))
c.close() 
conn.commit()

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