Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Throwing error while creating tables in SQL

I am currently executing a code, trying to create tables:

cur.executescript('''
    CREATE TABLE User(
        id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE ,
    name TEXT, email_id TEXT);

    CREATE TABLE Courses(
        id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    course_name TEXT);

    CREATE TABLE Member(
        user_id INTEGER, course_id INTEGER, role INTEGER)/*student-0 teacher 1*/
    PRIMARY KEY(user_id, course_id);
''')

It gives me this error:

File "E:\python_folder_practice_app_programming\SQLite\beginner_pro_db\beginner_pro_db.py", line 18, in
cur.executescript(”’
sqlite3.OperationalError: near "PRIMARY": syntax error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can someone help me debug this?

>Solution :

This is the problematic statement

CREATE TABLE Member(
    user_id INTEGER, course_id INTEGER, role INTEGER)/*student-0 teacher 1*/
PRIMARY KEY(user_id, course_id);

Your primary key constraint should be inside the parens (and separated by comma)

Like this (I’ve broken it into several lines for readability)

CREATE TABLE Member(
    user_id INTEGER, 
    course_id INTEGER, 
    role INTEGER, /*student-0 teacher 1*/
    PRIMARY KEY(user_id, course_id)
);

See examples here https://www.sqlitetutorial.net/sqlite-create-table/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading