I'm in the process of transferring my brain from PHP + MySQL to Python and at the moment I'm using sqlite as an intermediary to something else later because it's built in.
When I try to set up an initial database to work with, it throws an error on the INSERT
statement that INSERT
s multiple rows:
import sqlite3
query = """CREATE TABLE IF NOT EXISTS "users" (
"userID" INTEGER PRIMARY KEY autoincrement,
"email" varchar(255) NOT NULL,
"forename" varchar(50) NOT NULL,
"surname" varchar(50) NOT NULL
);
INSERT INTO "users" ("userID", "email", "forename", "surname")
VALUES
(1, 'user1@example.com', 'Test', 'User'),
(2, 'user2@example.com', 'Test', 'User');"""
db_conn = sqlite3.connect('test.db')
try:
db_conn.cursor().executescript(query)
db_conn.commit()
except:
print "error with database"
raise
finally:
db_conn.close()
When I run it, I get:
error with database
Traceback (most recent call last):
File "dbTest.py", line 20, in <module>
db_conn.cursor().executescript(query)
sqlite2.OperationalError: near ",": syntax error
It took me quite a while to find exactly which "," it didn't like (I mean, really? SQL errors suck!). It is indeed the "," separating INSERT
rows.
What stupid thing did I do?