Ich versuche, Daten für ein Lagerverwaltungssystem für ein Geschäft zu schaffen, das Weihnachtsartikel verkauft (ja, ich weiß Weihnachten ist weg, aber das ist die Aufgabe). Mein Code ist wie folgt:sqlite3.OperationalError: in der Nähe von "ProductID": Syntaxfehler
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)
Allerdings, wenn ich es laufen, bekomme ich diese Fehlermeldung:
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
Was hier los ist, und wie können diese gelöst werden? (Denken Sie daran, ich bin ein A-Level-Student, so dass alle Überlegungen hinter Ihrer Lösung und mein Problem ist sehr hilfreich!)
EDIT: Es sind noch keine Daten in der Tabelle. Dies wird später hinzugefügt.
So basic! Ich hätte es besser wissen sollen. –