2016-12-28 11 views
0

クリスマスアイテムを販売する店舗の在庫管理システム用のデータを作成しようとしています(はい、クリスマスは終わったのですが、それが課題です)。私のコードは以下の通りです:sqlite3.OperationalError:near "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レベルの生徒ですので、あなたの解答と私の問題の裏づけは非常に助かります!)

EDIT:まだテーブルにデータがありません。これは後で追加されます。

答えて

0

例外は、SQLite構文エラーであることを示しています。だから、基本的な

sql = """create table Product 
     (ProductID integer, 
     Name text, 
     Price real, 
     primary key(ProductID))""" 
+0

:あなたはdocumentationに対するあなたのCREATE TABLE文を比較すれば確かに、あなたは構文のような列定義、周りに括弧が必要であることを見つけることができます!私はよく知っていたはずです。 –

関連する問題