A. Cola
A. Cola

Reputation: 49

Sqlite/Python simple issue when searching DB

Hello I am having a bit of a dumbo problem. I am trying to search a DB for an ID number. The problem I am running into in when I enter a ID number that does not exist the program crashes. I will post my code under and try to explain where I am at. I am using SQlite with Python3.

cur = con.cursor()

cur.execute("SELECT * FROM coupons WHERE id_num=:id_num",
    {"id_num": input_value})
con.commit()
row = cur.fetchone()

#Verifying that the ID exists for later in the program. 
if input_value == row[0]:
    bad_code = False

I know my programming is somewhat amateurish but I am still learning the ropes.

The error is as follows:

Traceback (most recent call last):
File "Z:/Python_Programs/data_bases/coupon_scanner_v8.py", line 217, in      <module>
verify_barcode(user_code);
 File "Z:/Python_Programs/data_bases/coupon_scanner_v8.py", line 87, in verify_barcode
startDay = row[1]
TypeError: 'NoneType' object is not subscriptable

I will continue to do research to solve this problem. Thank you!!!

Upvotes: 1

Views: 228

Answers (1)

Mureinik
Mureinik

Reputation: 311188

fetchone() returns None if there are no more values to fetch from the cursor - in your case, where the id doesn't exist. So, in order to check if the id existed you should do something down these lines:

row = cur.fetchone()

# Verifying that the ID exists for later in the program. 
if row is not None:
    bad_code = False

Upvotes: 1

Related Questions