python - 如何计算数据库行数?
问题描述
嘿,我需要一些帮助我正在制作订单系统,我需要帮助
这是我需要的:我需要它来读取行中的值,因此如果订单完成则值为 2 并打印出订单已完成如果订单仍处于活动状态则该值应为 1 并打印出订单仍然有效
长话短说,我需要帮助来检索号码并确定订单是有效订单还是完整订单
这就是我所拥有的
sqx = "SELECT Customer_Status FROM Customer WHERE Username = 'Test' AND order_Status ='1'"
cursor.execute(sqx)
result = cursor.fetchall()
if result == '2':
print('Order is complete')
else:
print('order is active')
解决方案
在它的核心,你想使用 fetchone() 并拉出元组。此外,您确实应该开始使用参数替换来防止将来出现任何安全问题。
# It's always a good ide to use parameter substitution to prevent
# security issues. There aren't any here, but this is generally
# good practice
sqx = "SELECT Customer_Status FROM Customer WHERE Username = ? AND order_Status = ?"
cursor.execute(sqx, ('Test', '1'))
# We only want to look at one row, so use fetchone instead of
# fetchall. And in that row, go ahead and pull out the first
# value out of the tuple that's returned
result = cursor.fetchone()
if result is None:
# Either the user or order status is invalid
print("No match for query")
else:
result = result[0]
# And note here: Is the database storing '1' and '2' as strings instead of integers?
# If it is, this is fine, but if this field is an integer,
# use "result == 2" instead of "result == '2'"
if result == '2':
print('Order is complete')
else:
print('order is active')
推荐阅读
- python - imshow 的奇怪 cv2 行为
- reactjs - 如何在没有 SCSS 文件的情况下使用 SCSS 设置 div 样式对 React 组件不重要
- c# - C# Net Core:如何使用适当的实体跟踪实现域层
- java - 在 MAC OSX 中出现错误“错误:无法找到或加载主类 A 原因:java.lang.ClassNotFoundException:A”
- php - 邮寄独特的邮件(Laravel)
- html - 如何修复复选框
- laravel - Laravel 的 Vuedraggable 问题
- firebase - 如何在不访问 Deploy 命令返回的 URL 链接的情况下运行数据库触发器
- ms-word - 使用 pandoc 从 word 到 Latex ,引用问题
- python - 从单词列表中逐个字母打印