1

I'm trying to return 'search not found' when nothing is found in the database, but the program just jumps out the loop

I tried checking for 0 or '', but realized that the database returns NoneType. I'm checking for None now, but the code still does not get executed.

def search():
    def searchmenu():
        print('')
        ans=str(input('Make another [s]earch or return to the [m]ain menu: '))
        ans=ans.lower()
        if ans=='s':
            Inventory.search()
        elif ans=='m':
            menu=MainMenu()
            menu.menuops()   
        else:
            print('invalid menu option')
            searchmenu()

        mydb = mysql.connector.connect(host="localhost", user="root", password="", database="pharmd")
        cursor = mydb.cursor()
        query=str(input("Search: "))
        if query != "":
            cursor.execute('SELECT * FROM drugs WHERE name LIKE "%'+query+'%"')
            results=cursor.fetchall()
            if results != None:
                for data in results:
                    print('found!')
                    print(data)
                    searchmenu()
            else:
                print('No results found')
        else:
            print('Please enter a valid search!')
            print('')
            Inventory.search()
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Alyssa
  • 67
  • 7

1 Answers1

1

The problem is that cursor.fetchall() doesn't return None but an empty tuple ().

You can check cursor.rowcount for empty result according to the answer for this question.

Malekai
  • 4,765
  • 5
  • 25
  • 60
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40