python-3.x - 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
为什么会这样,我该如何解决?只是我愚蠢还是有特定的方法?
提前致谢。
解决方案
用这个:
result = cursor.fetchone()[0]
print (result)
return (result)
第一个fetchone()
只取了你需要的第一行然后把它扔掉了,所以fetchone()
你使用的第二个没有取到任何东西。
推荐阅读
- ios - 在保持底部填充的同时更改中心 Google 地图位置
- python - Python为mysql创建动态选择语句
- java-8 - 将 Stream 转换为 IntStream
- powerbi - 在 Power BI 中评论报表
- go - 将字符串传递给 Syscall(uintptr)
- ignite - 如何安排 Apache Ignite 的自定义备份
- asp.net-mvc - 如何部署在区域中有多个项目的 asp.net mvc 4 应用程序?
- vue.js - 切换活动类 Vue js
- selenium - 从 Instagram 下载公共数据进行研究
- angular - ck编辑器反应形式角度6不起作用