Home > Software engineering >  Throwing error while creating tables in SQL
Throwing error while creating tables in SQL

Time:09-04

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

Can someone help me debug this?

CodePudding user response:

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/

  • Related