创建表时出现“ sqlite3.OperationalError:在“)附近:语法错误”

时间:2019-03-15 03:16:48

标签: python sql sqlite syntax-error operationalerror

我正在尝试为自己的社区制作一个股票程序,并且正在尝试运行以下代码:

def create_tables():
    c.execute('CREATE TABLE IF NOT EXISTS products(cod INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, bar_code INTEGER, '\
              'descr VARCHAR(100) NOT NULL, quant_stock REAL, type_un TEXT, cost_un REAL, value_un REAL NOT NULL, fab_date TEXT, '\
              'due_date TEXT)')

    c.execute('CREATE TABLE IF NOT EXISTS sells(cod INTEGER NOT NULL, bar_code INTEGER, '\
              'descr TEXT PRIMARY KEY NOT NULL, value_un REAL NOT NULL, type_un TEXT, quant_stock REAL NOT NULL, '\
              'tot_value REAL NOT NULL, sell_date TEXT NOT NULL, FOREIGN KEY (cod) REFERENCES products(cod)')

    c.execute('CREATE TABLE IF NOT EXISTS purchase(cod INTEGER FOREIGN KEY, bar_code INTEGER, '\
              'descr TEXT NOT NULL, cost_un REAL, type_un TEXT, pur_quant REAL NOT NULL, tot_cost REAL NOT NULL, '\
              'pur_date TEXT NOT NULL, fab_date TEXT, due_date TEXT')

我收到此错误:

  File "app.py", line 17, in create_tables
    c.execute('CREATE TABLE IF NOT EXISTS sells(cod INTEGER NOT NULL, bar_code INTEGER, '\
sqlite3.OperationalError: near ")": syntax error

我不知道为什么会这样,因为创建的第一个表没有问题(我已经注释了另外两个表并且脚本正常运行)。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您在第二个和第三个表插入项中缺少右括号:

def create_tables():
    c.execute('CREATE TABLE IF NOT EXISTS products(cod INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, bar_code INTEGER, '\
              'descr VARCHAR(100) NOT NULL, quant_stock REAL, type_un TEXT, cost_un REAL, value_un REAL NOT NULL, fab_date TEXT, '\
              'due_date TEXT)')

    c.execute('CREATE TABLE IF NOT EXISTS sells(cod INTEGER NOT NULL, bar_code INTEGER, '\
              'descr TEXT PRIMARY KEY NOT NULL, value_un REAL NOT NULL, type_un TEXT, quant_stock REAL NOT NULL, '\
              'tot_value REAL NOT NULL, sell_date TEXT NOT NULL, FOREIGN KEY (cod) REFERENCES products(cod))')

    c.execute('CREATE TABLE IF NOT EXISTS purchase(cod INTEGER FOREIGN KEY, bar_code INTEGER, '\
              'descr TEXT NOT NULL, cost_un REAL, type_un TEXT, pur_quant REAL NOT NULL, tot_cost REAL NOT NULL, '\
              'pur_date TEXT NOT NULL, fab_date TEXT, due_date TEXT)')