如何在将mysql数据库中的数据提取到python中时编写检查空集的语句?

2024-03-29 00:17:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从用户那里获取关于他们的公交路线的“from”和“where to”的条目。然后我需要在MySQL中检查我已经创建的表是否包含该对。我应该用python编写什么语句来检查我获取的数据是否是一个空集,并使用“except”关键字告诉用户该对数据的条目不存在

我不知道如何使用“except”关键字。假设用户输入“from drih”和“where to Rajasthan”,并且特定条目确实存在于我的表中,我想知道应该编写的python语句/代码,以便用户知道它不存在

import MySQLdb
def passenger():
    db=MySQLdb.connect(host="localhost",port=3306,user="root",passwd="",db="superprogram")
    cur=db.cursor()                                     #connected with MySQL
    num=int(input('''Enter 1 to book tickets
    Enter 2 to cancel tickets
    Enter 3 for enquiry panel-'''))
    if num==1:
        start=input("Enter the name of your state-")        #where you live
        dest=input("Enter your destination-")              #your destination
        start.capitalize()
        dest.capitalize()               #because MySQL entries are capital
        s="select * from businfo where Point_Of_Departure='"+start+"' and Destination='"+dest+"'"
        cur.execute(s)
        P=cur.fetchall()
        for i in P:                                 #if entry exists
             if i[6]>0:                              #if there are seats left
                print("Bus ID-",i[1])
                print("Point Of Departure-",i[2])
                print("Destination-",i[3])

如果“p”不包含任何内容,如何通知用户特定对不存在


Tags: to用户frominputdbyourifmysql