import sqlite3
conn=sqlite3.connect('food.db')
print("Opened Database Successfully")
conn.execute('''CREATE TABLE ORDER
(ORDERID INT PRIMARY KEY NOT NULL,
FOODID INT NOT NULL,
CUSTOMERID INT NOT NULL,
DATEOFORDER CHAR(9) NOT NULL,
PRICEOFORDER REAL NOT NULL,
FOREIGN KEY (CUSTOMERID) REFERENCES CUSTOMER(CUSTID),
FOREIGN KEY (FOODID) REFERENCES FOOD(FOODID));''')
conn.close()
创建此代码,我收到错误消息作为操作错误。
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
sqlite3.OperationalError: near "ORDER": syntax error
已创建客户和食品表,它们都有效
答案 0 :(得分:0)
ORDER
是SQL中的保留字,您需要引用它才能将其用作表名。将它放在双引号中:
conn.execute('''CREATE TABLE "ORDER"
(ORDERID INT PRIMARY KEY NOT NULL,
FOODID INT NOT NULL,
CUSTOMERID INT NOT NULL,
DATEOFORDER CHAR(9) NOT NULL,
PRICEOFORDER REAL NOT NULL,
FOREIGN KEY (CUSTOMERID) REFERENCES CUSTOMER(CUSTID),
FOREIGN KEY (FOODID) REFERENCES FOOD(FOODID));''')
您也可以使用[ORDER]
和`ORDER`
,但这些使用非标准语法。