I'm attempting to insert multiple rows into an SQLite DB with python. I've got three tables:
UniversityA:
- id
- course_name
- course_code
- course_prefix
UniversityB:
- id
- course_name
- course_code
- course_prefix
CourseMap:
- universityA_id (references id from universityA)
- universityB_id (references id from universityB)
- is_flagged (boolean 0/1)
I can add a single record to the CourseMap table easily with this syntax:
cur.execute('INSERT INTO CourseMap (universityA_id, universityB_id, is_flagged) VALUES ( (SELECT id from universityA WHERE course_code=301 AND course_prefix="AERO"), (SELECT id from universityB WHERE course_code=101 AND course_prefix="ARCH"), 0)')
But when I try to incorporate this statement into a list to execute multiple inserts using executemany(), I get a syntax error:
equivs = [
((SELECT id from universityA WHERE course_code=301 AND course_prefix="AERO"), (SELECT id from universityB WHERE course_code=101 AND course_prefix="ARCH"), 0),
((SELECT id from universityA WHERE course_code=301 AND course_prefix="AERO"), (SELECT id from universityB WHERE course_code=101 AND course_prefix="ARCH"), 0),
((SELECT id from universityA WHERE course_code=301 AND course_prefix="AERO"), (SELECT id from universityB WHERE course_code=101 AND course_prefix="ARCH"), 0)
]
# Fill the table
cur.executemany('INSERT INTO courseMap (universityA_id, universityB_id, is_flagged) VALUES (?,?,?)', equivs)
The error I'm getting is "syntax error: invalid syntax", with the caret positioned right before the boolean is_flagged value on the first insert.
Am I missing something in the multiple insert syntax on executemany()? My SQLite 3 version is 3.10.0.
equivsdeclaration supposed to be strings?INSERTstatement they'll be plugged into is a string...do you mean the items inequivsshould be in quotes?executemanystatement.SELECTand after each"AERO"and"ARCH".