简体   繁体   English

(Python)cursor.execute(sql)

[英](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." 

The first function is used to make the table and it shows my column titles, the second function is needed to update a specific value in the table. 第一个函数用于制作表,并显示我的列标题,第二个函数用于更新表中的特定值。

But when this is ran the inputs are executed, however when all show all the products in the table the value for stock level doesn't change. 但是运行此命令后,将执行输入,但是当所有输入均显示表中的所有产品时,库存水平的值不会改变。

So I think the problem has something to do with the cursor.execute(sql) line. 因此,我认为问题与cursor.execute(sql)行有关。

Yes; 是; you're passing literal strings, instead of the values returned from your input calls. 您传递的是文字字符串,而不是您的输入调用返回的值。 You need to use parameters in the statement and pass thme to the execute call. 您需要在语句中使用参数,并将thme传递给execute调用。

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

还是类似的东西?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM