0

I want to learn how to build sqlite with python, after following the tutorial, I keep find this error, I do search on stackoverflow but after checking few times I still can't figure out what happened...

Here's my code:

import sqlite3
import csv

conn = sqlite3.connect("member_2.db")
cursor = conn.cursor()

# build db table
 sql_command="""
    CREATE TABLE member (
    id PRIMARY KEY INTEGER AUTOINCREMENT,
    name TEXT NOT NULL,
    gender CHAR(1),
    score INTEGER,
    team TEXT
    );

    CREATE TABLE pick_history (
      member_id INTEGER,
      pick_time DATE DEFAULT (datatime('now', 'localtime')),
      FOREIGN KEY(member_id) REFERENCES member(id)
    );
 """

cursor.execute(sql_command)

Please help~Thanks!

1 Answer 1

2

You need to specify the data type of id prior to setting it as a primary key. For example, the following will work:

sql_command="""
    CREATE TABLE member (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    gender CHAR(1),
    score INTEGER,
    team TEXT
    );"""

Thanks to @kindall for linking to the docs making it clear that the column type is specified before the column constraint.

Also, thanks to @PRMoureu for pointing out that once you fix that error you will then get the warning:

Warning: You can only execute one statement at a time.

So split the query up into two statements and create the tables separately.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep, column type comes before column constraints. sqlite.org/syntax/column-def.html
@PRMoureu Thanks, was about to add but then switched to edit in the link from kindall :) Joint effort!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.