如何根据用户条目从SQL表中删除项?

2024-10-02 12:22:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在MySQL数据库中存储项目、URL和价格,并希望用户能够根据名称删除项目。我试图通过使用DELETE sql语句来实现这一点,但它只返回“0 item removed”,我猜它无法找到条目,不管是它没有接受输入还是什么。任何帮助都将不胜感激,因为我真的很想掌握这个窍门

def deleteItem(): 
    goneItem = input("Enter item: ")
    removeItem = "DELETE FROM amazon_items WHERE itemName = '%s'"
    mycursor.execute(removeItem, goneItem)
    db.commit()
    print(mycursor.rowcount, "item removed")


Tags: 项目用户名称数据库urlsqlmysql价格
2条回答

尝试删除%s周围的单引号,您应该将%s的值作为元组传递

def deleteItem(): 
    goneItem = input("Enter item: ")
    removeItem = "DELETE FROM amazon_items WHERE itemName = %s"
    mycursor.execute(removeItem, (goneItem,))
    db.commit()
    print(mycursor.rowcount, "item removed")

嗨,LearningCode欢迎来到stackoverflow

mycursor.execute(removeItem, (goneItem,))

试试这个

您可以在文档的here中看到,您需要包含一个逗号,以使第二个参数成为元组

相关问题 更多 >

    热门问题