Here I’m basically trying to fetch a string value from the SQL Table and I want to compare that value with the string I entered. I tried this simple way but unfortunately, it isn’t working. I need help.
JavaScript
x
12
12
1
def cantkt():
2
#tkt()
3
#canstate=("DELETE FROM Chart WHERE PNR=%s"%canpnr)
4
canpnr=input("Enter PNR No. to Cancel Ticket: ")
5
mycsr.execute("SELECT PNR FROM Chart WHERE PNR=%s"%canpnr)
6
res=mycsr.fetchone()
7
print(res)
8
if res==canpnr:
9
print("PNR MATCHED")
10
else:
11
print("PNR No. DOES NOT MATCH")
12
Advertisement
Answer
as you can see in the print, the result of the fetching is a tuple, not a string.
You can do simply this:
JavaScript
1
3
1
res = mycsr.fetchone()
2
if res: res = res[0]
3
Explanation:
res is (‘1019654419’, ) so you must seek the value you want to get, in this case the first one.
In “if res: res = res[0]” we check if res has a value because in case the ticket does not exist the fetching value would be an empty tuple, so trying to get the first result will raise an IndexError.