-2

This functions searches for records in my database. Apart from if condition the else statement ( "Record not found") also keeps on executing even if the condition is true. 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")
  • 1
    Why are you not using the `WHERE` statement of SQL to search in database. It would be a lot faster. – Yoshikage Kira Jun 10 '21 at 05:57
  • try executing the code without try and except so u can catch errors –  Jun 10 '21 at 05:58
  • 2
    Why do you set i to list(i) when you are already setting i to the items in s? That doesn't make any sense to me. – The Grand J Jun 10 '21 at 05:58
  • 1
    You probably meant to use [`for .. else` with a `break`](https://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops) if you are going to iterate through all records returned like this. – metatoaster Jun 10 '21 at 05:59

2 Answers2

0
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")
0

You don't even need the for loop. You can perform the search just by using SQL statements which is way faster than the iterative method you are using. Assuming the column name is 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")

If you really insist on using the iterative method, then the mistake you were making was the line i = list(i). i is already a list and by putting i in list you are making list of lists (2D list) which messes up your iteration.

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")

Also, use better variable names.

Yoshikage Kira
  • 1,070
  • 1
  • 12
  • 20