python - Reducing item in SQLite DB by a certain percentage
问题描述
I'm trying to take a user input to reduce the price of an item in my database by the user input as a percentage.
currently I'm getting the error:
currentPriceRowFetch = currentPriceSelect.fetchone()
AttributeError: 'str' object has no attribute 'fetchone'
However I would not have expected currentPriceSelect to be read as a string as it should be an SQL statement. Not sure where I've gone wrong here.
elif userinput == "6":
print(pd.read_sql_query("SELECT * FROM Products", conn))
itemToReduce = input("Enter the id of the item to put on sale: ")
salePercentage = input("Enter the percentage to reduce the item by: ")
itemSale = '''UPDATE Products
SET ProductPrice = ?
WHERE ProductID = ?
'''
currentPriceSelect = "SELECT * FROM Products WHERE ProductID = ?"
conn.execute(currentPriceSelect, itemToReduce)
currentPriceRowFetch = currentPriceSelect.fetchone()
currentPrice = currentPriceRowFetch[3]
salePercentage = salePercentage / 100
newCost = currentPrice - (currentPrice * salePercentage)
conn.execute(itemSale, (newCost, itemToReduce))
conn.commit()
print("Old price was " + currentPrice)
print("New price is " + newCost)
I have changed the currentPriceSelect
to this:
currentPriceSelect = conn.execute("SELECT * FROM Products WHERE ProductID = ?")
Now I am getting a new error:
currentPriceSelect = conn.execute("SELECT * FROM Products WHERE ProductID = ?")
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 0 supplied.
Updated code:
elif userinput == "6":
print(pd.read_sql_query("SELECT * FROM Products", conn))
itemToReduce = input("Enter the id of the item to put on sale: ")
salePercentage = input("Enter the percentage to reduce the item by: ")
itemSale = '''UPDATE Products
SET ProductPrice = ?
WHERE ProductID = ?
'''
currentPriceSelect = conn.execute("SELECT * FROM Products WHERE ProductID = ?"), itemToReduce
currentPriceRowFetch = currentPriceSelect.fetchone()
currentPrice = currentPriceRowFetch[3]
salePercentage = salePercentage / 100
newCost = currentPrice - (currentPrice * salePercentage)
conn.execute(itemSale, (newCost, itemToReduce))
conn.commit()
print("Old price was " + currentPrice)
print("New price is " + newCost)
解决方案
推荐阅读
- node.js - 使用带有可下载在线链接的归档模块
- theano - 如何在 Theano 中创建多对角方阵?
- java - Spring Boot 安全主体用户更改为新登录的用户详细信息
- kubernetes - 如何连接到官方 helm chart 创建的 OpenLDAP?
- javascript - GraphQL 查询省略了不同类型对象的空字段
- java - 缺少 Android Studio 设计器视图
- networking - Prometheus pod 无法调用 apiserver 端点
- javascript - JS / TS中的“a | = b”如果开始有效,是什么意思?
- vue.js - 浏览器不呈现更新的值,但 Vue DevTools 会
- python-3.x - 无法在第二个 Tkinter 帧中显示图像标签