(Python) cursor.execute(sql)


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." 
第一个函数用于

制作表格并显示我的列标题,第二个函数用于更新表中的特定值。

但是当

运行此命令时,将执行输入,但是当所有产品都显示表中的所有产品时,库存水平的值不会更改。

所以我认为这个问题与 cursor.execute(sql( 行有关。

或者类似的东西?

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

是的;您传递的是文字字符串,而不是从输入调用返回的值。您需要在语句中使用参数并将其传递给执行调用。

sql= "update product set StockLevel = %s where ProductID = %s;"
cursor.execute(sql, (Stock_Update, Product_ID))

最新更新