user4714953
user4714953

Reputation:

Inserting list into sqlite table column python3

I've been looking for an answer online and none seem to fix my issue. I'm getting the following error:

line 17, in <module>
    c.execute('INSERT INTO semesters (semester) VALUES (?)',mystuff)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current 
  statement uses 1, and there are 3 supplied.

I've made a lot of changes in many places but i still keep getting either this error, another error, or it works, but it won't insert data properly.

Here is my code:

import sqlite3

conn = sqlite3.connect('scheduler.db')
c = conn.cursor()
c.execute('''DROP TABLE IF EXISTS semesters''')
c.execute('''CREATE TABLE semesters
                   (idse INTEGER PRIMARY KEY, semester TEXT, Idc INTEGER)''')
                     #FOREIGN KEY (Idc) REFERENCES careers (idc)

mystuff = [('Semester 1'),
           ('Semester 2'),
           ("Semester 3"),]

c.execute('INSERT INTO semesters (semester) VALUES (?)',mystuff)
conn.commit()

Upvotes: 1

Views: 6125

Answers (1)

user4113344
user4113344

Reputation:

As python sqlite3 documentation suggests, use the non-standard shortcut sqlite3.Connection.executemany() instead of just execute():

mystuff = [('Semester 1'),
           ('Semester 2'),
           ("Semester 3"),]

c.executemany('INSERT INTO stocks VALUES (?)', mystuff)
conn.commit()

Upvotes: 3

Related Questions