首页 > 解决方案 > python - 不能使用sqlite数据

问题描述

我遇到了一个问题,我可以从 sqlite 打印下标数据,但不能将其返回或将其分配给变量。在 Windows 10 中使用 python3 和 sqlite。

def dbFindQuestion():
question = 'red'
sql = "SELECT * FROM words WHERE English=? OR German=?"
cursor.execute(sql, [(question), (question)])
print (cursor.fetchone()[0]) #this works
return (cursor.fetchone()[0]) #this doesn't
abc = (cursor.fetchone()[0]) # this doesn't

从单词表中打印第一列会打印“红色”,这是正确的但是,当我将其返回或将其设置为变量时,它会给我错误:

TypeError: 'NoneType' object is not subscriptable

为什么会这样,我该如何解决?只是我愚蠢还是有特定的方法?

提前致谢。

标签: python-3.xsqlite

解决方案


用这个:

result = cursor.fetchone()[0]
print (result)
return (result)

第一个fetchone()只取了你需要的第一行然后把它扔掉了,所以fetchone()你使用的第二个没有取到任何东西。


推荐阅读