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')

我得到这个错误:

  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

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

4

1 回答 1

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)')
于 2019-03-15T03:21:50.853 回答