1

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()
4
  • What about 'INSERT INTO semesters (semester) VALUES (?, ?, ?)' ? Commented Apr 27, 2015 at 17:11
  • Just tried it, won't work. I'm telling it to explicitly insert into column named semester, i think that's why i can only use one '?' because it is only one column. Commented Apr 27, 2015 at 17:15
  • Damn'it! What was I looking at :D . You need 3 distinct INSERT statements since you want 3 records in fact. Commented Apr 27, 2015 at 17:17
  • Would you mind doing an example? And if it works i can pick you as accepted answer:p Commented Apr 27, 2015 at 17:29

1 Answer 1

3

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.