python查询数据库,打印查询结果过程中出现'NoneType' object is unsubscriptable

问题再现,最后一行红字部分出现问题。虽然结果也一行行打印出来了。但是结果却报错,为什么捏?虽然python中的while True不会使程序陷入死循环,但是当将查询结果(下边查询语句返回6条记录)全部打印出来,继续往下打印查询结果时,这时row中存放的是空值。

>>> cursor_pet.execute('select userid,petid from t_pp_pet_01') 
>>> while True:
...     row =  cursor_pet.fetchone()
...     print row[0],' ',row[1] 
... 
1   11
1   12
2   11
3   11
2   10
4   15
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
TypeError: 'NoneType' object is unsubscriptable


 

程序更正,在程序中加入判断if row == None:break,然后上边的问题解决

>>> cursor_pet.execute('select userid,petid from t_pp_pet_01')
>>> while True:
...     row =  cursor_pet.fetchone()
...     if row == None:
...             break
...     print row[0],' ',row[1]
... 
1   11
1   12
2   11
3   11
2   10
4   15


 

你可能感兴趣的:(python查询数据库,打印查询结果过程中出现'NoneType' object is unsubscriptable)