1
votes

I'm just trying out some Python3-sqlite3 code when the following error occured

import sqlite3
conn=sqlite3.connect("../db/mydb.db")
conn.execute('''CREATE TABLE SCHEDULER IF NOT EXISTS (SNO INTEGER PRIMARY KEY AUTOINCREMENT, STRTIME TEXT, ENDTIME TEXT, MODE TEXT)''')
conn.execute('''INSERT INTO SCHEDULER VALUES (1, 'XXXX', 'XXXX', 'MODE')''')
conn.close()

I get the error

Traceback (most recent call last): File "db.py", line 4, in conn.execute('''CREATE TABLE SCHEDULER IF NOT EXISTS(SNO INTEGER PRIMARY KEY AUTOINCREMENT, STRTIME TEXT, ENDTIME TEXT, MODE TEXT)''') sqlite3.OperationalError: near "IF": syntax error

2

2 Answers

2
votes

The IF NOT EXISTS must come earlier, like this:

CREATE TABLE IF NOT EXISTS SCHEDULER  (SNO INTEGER PRIMARY KEY AUTOINCREMENT, STRTIME TEXT, ENDTIME TEXT, MODE TEXT);

If this still doesn't work, then your SQLite version is really old (older than version 3.3.0).

0
votes

SCHEDULER being the name of your table, it needs to be in quote like "SCHEDULER" IF