【问题标题】:Code not being executed, program jumps out of the loop and waits for another command代码没有被执行,程序跳出循环,等待另一个命令
【发布时间】:2019-09-13 21:56:04
【问题描述】:

当在数据库中找不到任何内容时,我试图返回“未找到搜索”,但程序只是跳出循环

我尝试检查0'',但发现数据库返回NoneType。我现在正在检查None,但代码仍然没有被执行。

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

【问题讨论】:

  • 为什么search 里面有searchmenu,是错字吗?

标签: python-3.x


【解决方案1】:

问题是 cursor.fetchall() 不返回 None 而是一个空元组 ()

您可以根据the answerthis question检查cursor.rowcount的空结果。

【讨论】:

    猜你喜欢
    • 2019-08-17
    • 2020-12-12
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2020-07-02
    • 2020-04-20
    • 1970-01-01
    相关资源
    最近更新 更多