2016-03-22 6 views
0
def makeProductTable(): 
"""This creates a database with a blank table.""" 
with connect("products.db") as db: 
    cursor = db.cursor() 

    cursor.execute(""" 
     CREATE TABLE Product(
     ProductID integer, 
     GTIN integer, 
     Description string, 
     StockLevel integer, 
     Primary Key(ProductID));""") 
    db.commit() 

def editStockLevel(): 
with connect("products.db") as db: 
    cursor = db.cursor() 
    Product_ID=input("Please enter the id of the product you would like to change: ") 
    Stock_Update=input("Please enter the new stock level: ") 
    sql = "update product set StockLevel = ('Stock_Update') where ProductID = ('Product_ID');" 
    cursor.execute(sql) 
    db.commit() 
return "Stock Level Updated." 

最初の関数はテーブルを作成するために使用され、カラムタイトルが表示され、2番目の関数はテーブルの特定の値を更新するために必要です。(Python)cursor.execute

しかし、これを実行すると入力が実行されますが、すべてが表内のすべての製品を表示すると、在庫レベルの値は変更されません。

だから、問題はcursor.execute(sql)行と関係があると思います。

答えて

0

cur.execute("UPDATE Product set StockLevel = ? where ProductID = ?",(Stock_Update,Product_ID)) 
+0

ありがとうございます、これは私のコードを修正しました。 – JW2209

+0

@ JW2209。それが動作する場合は、答えとしてマークしてください。ありがとう。 – KR29

0

はい。入力呼び出しから返された値の代わりにリテラル文字列を渡しています。ステートメント内のパラメーターを使用して、実行呼び出しにthmeを渡す必要があります。

sql= "update product set StockLevel = %s where ProductID = %s;" 
cursor.execute(sql, (Stock_Update, Product_ID)) 
関連する問題