2015-09-12 66 views
1

你好,我有一點小問題。我正在嘗試在數據庫中搜索ID號碼。當我輸入一個不存在程序崩潰的ID號碼時,我遇到了這個問題。我將發佈我的代碼並嘗試解釋我所在的位置。我正在使用SQlite和Python3。搜索DB時Sqlite/Python簡單問題

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 

我知道我的編程有點業餘,但我仍然在學習繩索。

的錯誤如下:

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 

我會繼續做研究,以解決這個問題。謝謝!!!

回答

1

fetchone()返回None如果沒有更多的值從光標獲取 - 在你的情況下,id不存在。因此,爲了檢查身份證是否存在,您應該按照以下方式進行操作:

row = cur.fetchone() 

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

傻瓜.....謝謝您,先生! –