【问题标题】:Else statement keeps executing PYTHONElse 语句继续执行 PYTHON
【发布时间】:2021-06-10 05:54:20
【问题描述】:

此函数在我的数据库中搜索记录。除了 if 条件之外,即使条件为真,else 语句(“找不到记录”)也会继续执行。 OUTPUT screenshot

def displaySearchAcc():
try:
    command = "SELECT * FROM BANK"
    mycursor.execute(command)
    s = mycursor.fetchall()
    ch = input("Enter the account number to be searched : ")
    for i in s:
        i = list(i)
        if i[0] == ch:
            print("*" * 125)
            print("ACCNO", "NAME", "MOBILE", "EMAIL", "ADDRESS", "CITY", "COUNTRY", "BALANCE")
            print("=" * 125)
            for j in i:
                print("%14s" % j, end=' ')
                print()
        else:
            print("record not found")
except:
    print("Table not found")

【问题讨论】:

  • 为什么不使用SQL的WHERE语句在数据库中搜索。会快很多。
  • 尝试在没有尝试的情况下执行代码,除了这样你可以捕获错误
  • 既然已经将 i 设置为 s 中的项目,为什么还要将 i 设置为 list(i)?这对我来说没有任何意义。
  • 如果您要遍历所有这样返回的记录,您可能打算使用for .. else with a break

标签: python sql control-flow try-except


【解决方案1】:
def displaySearchAcc():
try:
    command = "SELECT * FROM BANK"
    mycursor.execute(command)
    s = mycursor.fetchall()
    ch = input("Enter the account number to be searched : ")
    for i in s:
        if i[0] == ch:
            print("*" * 125)
            print("ACCNO", "NAME", "MOBILE", "EMAIL", "ADDRESS", "CITY", "COUNTRY", "BALANCE")
            print("=" * 125)
            for j in i:
                print("%14s" % j, end=' ')
                print()
        else:
            print("record not found")
except:
    print("Table not found")

【讨论】:

    【解决方案2】:

    你甚至不需要 for 循环。您只需使用 SQL 语句即可执行搜索,这比您使用的迭代方法要快得多。假设列名是 account_number

    def displaySearchAcc():
        account_no = input("Enter the account number to be searched : ")
        command = f"SELECT * FROM BANK WHERE account_number = '{account_no}'"
        row = mycursor.fetchone()
        if row is not None:
            print("*" * 125)
            print(
                "ACCNO", "NAME", "MOBILE", "EMAIL", "ADDRESS", "CITY", "COUNTRY", "BALANCE"
            )
            print("=" * 125)
            for col in row:
                print("s" % col, end=" ")
                print()
        else:
            print("Record not found")
    
    

    如果你真的坚持使用迭代方法,那么你犯的错误就是i = list(i)这行。 i 已经是一个列表,通过将 i 放入列表中,您正在制作列表列表(2D 列表),这会扰乱您的迭代。

    def displaySearchAcc():
        try:
            command = "SELECT * FROM BANK"
            mycursor.execute(command)
            all_rows = mycursor.fetchall()
            account_no = input("Enter the account number to be searched : ")
            for row in all_rows:
                if row[0] == account_no:
                    print("*" * 125)
                    print("ACCNO", "NAME", "MOBILE", "EMAIL", "ADDRESS", "CITY", "COUNTRY", "BALANCE")
                    print("=" * 125)
                    for col in row:
                        print("%14s" % col, end=' ')
                        print()
                    break; # No need to iterate further
            else:
                print("record not found")
        except:
            print("Table not found")
    

    另外,使用更好的变量名。

    【讨论】:

    • 非常感谢Goion,现在错误已解决。我会尝试使用更好的变量名:P
    • @AnkitDubey 接受答案。在 cmets 中无需感谢。
    猜你喜欢
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多