首页 > 解决方案 > 如何计算数据库行数?

问题描述

嘿,我需要一些帮助我正在制作订单系统,我需要帮助

这是我需要的:我需要它来读取行中的值,因此如果订单完成则值为 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')  

标签: pythonsqlite

解决方案


在它的核心,你想使用 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')

推荐阅读