pythonpython-3.xfitnesssqlite3-python

How to Resolve Syntax Error of the sqlite3.connect.cursor.execute() Function


I'm creating an exercise database Python script using the SQLite3 library & following a very helpful YouTube guide, & I'm running into issues near the very beginning of just creating the database in the first place. Please take a look at my code below:

import sqlite3

#connect = sqlite3.connect(':memory:') # If want database in memory.
con = sqlite3.connect('Exercise Database.db') # Connect to database.
c = con.cursor() # Create a cursor.

c.execute('''CREATE TABLE IF NOT EXISTS Exercises(
Name TEXT,
Force TEXT,
Equipment TEXT,
OneRM REAL,
Difficulty TEXT,
Primary TEXT,
Seconary TEXT,
Tertiary TEXT,
Animation BLOB
)''') # Create a table. The 5 datatypes are NULL, INTEGER, REAL, TEXT, & BLOB.

con.commit() # Commit our command.
con.close() # Close our connection.

Upon running the above block, I get the following error message:

Traceback (most recent call last):
  File "/Users/johncre752/Desktop/Exercise Database.py", line 49, in <module>
    c.execute('''CREATE TABLE IF NOT EXISTS Exercises(
sqlite3.OperationalError: near "TEXT": syntax error

I was expecting no errors to occur while running this, as I've closely examined my code in accordance w/ the tutorial as well as Python's own website for the library, & I can't seem to find any discrepancies that would warrant this. Looking online at others' posts related to this syntax error, I noticed all of them involved different reasons as to why they experienced that message. (Not enough size for memory, dealing w/ the "?" variable instead of this, etc.) I'm a newbie programmer, so I'd appreciate anyone able to identify the issue at hand. Thank you!


Solution

  • Primary is a reserved key in SQLite, you can check the keywords here:

    If you modify your query it works:

    CREATE TABLE IF NOT EXISTS Exercises(
    Name TEXT,
    Force TEXT,
    Equipment TEXT,
    OneRM REAL,
    Difficulty TEXT,
    `Primary` TEXT,
    Seconary TEXT,
    Tertiary TEXT,
    Animation BLOB
    )