0
我試圖爲一家銷售聖誕用品的店鋪創建一個庫存管理系統的數據(是的,我知道聖誕節已經過去了,但那是任務)。我的代碼如下:sqlite3.OperationalError:在「ProductID」附近:語法錯誤
import sqlite3
def existing_table(table_name,sql):
response = input("The table {0} already exists. Do you wish to recreate it? (Y/N)")
if response.upper() == "Y":
keep_table = False
print("The {0} table will be recreated. All existing data will be erased.".format(table_name))
cursor.execute("drop table if exists {0}".format(table_name))
db.commit()
elif response.upper() == "N":
print("The existing table was kept.")
else:
existing_table(table_name,sql)
if not keep_table:
cursor.execute(sql)
db.commit()
def create_table(db_name,table_name,sql):
with sqlite3.connect(db_name) as db:
cursor = db.cursor()
cursor.execute("select name from sqlite_master where name=?",(table_name,))
result = cursor.fetchall()
keep_table = True
if len(result) == 1:
existing_table()
cursor.execute(sql)
db.commit()
if __name__ == "__main__":
db_name = "XmasShop.db"
sql = """create table Product
ProductID integer,
Name text,
Price real,
primary key(ProductID)"""
create_table(db_name,"Product",sql)
然而,當我運行它,我得到這個錯誤信息:
Traceback (most recent call last):
line 36, in <module>
create_table(db_name,"Product",sql)
line 26, in create_table
cursor.execute(sql)
sqlite3.OperationalError: near "ProductID": syntax error
什麼是錯在這裏,以及如何加以解決? (請記住,我是第一年的A級學生,所以任何推理和解決方案背後的問題都是非常有幫助的!)
編輯:表中沒有數據。這將在稍後添加。
所以基本!我應該更清楚地知道。 –