I'm reviewing other people's code for creating a table using Sqlite3 in Python, and I think my code actually matches up but I keep getting an error. Can someone help me understand where I went wrong? Thank you!
import sqlite3
conn = sqlite3.connect('finances.db')
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS orders (ID INT, DATE TEXT, CHECK TEXT, AMOUNT REAL,DESC TEXT)")
conn.commit()
conn.close()
Error:
line 17, in <module>
c.execute("CREATE TABLE IF NOT EXISTS orders (ID INT, DATE TEXT, CHECK TEXT, AMOUNT REAL,DESC TEXT)")
sqlite3.OperationalError: near "TEXT": syntax error
CHECK
is a reserved word in SQLite. You need to surround it with double quotes, or better yet change the name of the column to something else, so you don't need to worry about this later on when writing queries.
CREATE TABLE IF NOT EXISTS orders (
ID INT,
DATE TEXT,
CHK TEXT,
AMOUNT REAL,
DESC TEXT
);
I would also advocate against columns names such as DATE
and DESC
; although SQLite allows that, they clearly correspond to SQL keywords, which makes them ambiguous by nature:
CREATE TABLE IF NOT EXISTS orders (
ID INT,
ORDER_DATE TEXT,
CHK TEXT,
AMOUNT REAL,
DESCR TEXT
);
Reference: SQLite keywords.