[英]Python/sqlite3 function compare : TypeError: 'NoneType' object is not subscriptable
[英]Python Sqlite3 TypeError: 'NoneType' object is not subscriptable
def get_coffee_name(coffee_type):
if coffee_type == "Hot":
conn = sqlite3.connect('coffee.db')
c = conn.cursor()
coffee_id = rand_coffee()
c.execute("SELECT tag FROM coffees WHERE id = '%s'" % (coffee_id))
while True:
coffee_type = c.fetchone()[0]
print(coffee_type)
if coffee_type == "Hot":
conn.close()
conn = sqlite3.connect('coffee.db')
c = conn.cursor()
c.execute("SELECT name FROM coffees WHERE id = '%s'" % (coffee_id))
return c.fetchone()[0]
break
退货
TypeError: 'NoneType' object is not subscriptable
我尝试了其他循环解决方案,但它也没有那样工作。
coffee_type
是冷的或热的,而不是任何时候都没有。
例如, cursor.fetchone() sqlite 调用返回一个元组,例如 (1,),并且您不能像使用列表那样使用索引或下标很好地引用它,所以请执行以下操作:
x = (1,)
y = list(x)
print(y[0])
1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.