python - SQLite 查询难度
问题描述
我有一个带有三个关系表的 SQLite 数据库。我正在尝试根据 ID 关系从日志表中返回最大记录以及来自其他表的相关列。
我在 DB Browser 中创建了查询并验证它返回了预期的记录,但是,当我在我的 python 代码中使用完全相同的查询语句时,它永远不会进入“for”循环。
python中的SQL语句-
def GetLastLogEntry():
readings = ()
conn = sqlite3.connect(dbName)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute("SELECT f.FoodCategory, f.FoodName, gs.FoodWeight,
gsl.GrillDateTime, gsl.CurrentGrillTemp, gsl.TargetGrillTemp,
gsl.CurrentFoodTemp, gsl.TargetFoodTemp, gsl.CurrentOutsideTemp,
gsl.CurrentOutsideHumidity FROM Food as f, GrillSession as gs,
GrillSessionLog as gsl WHERE f.FoodId = gs.FoodId AND
gs.GrillSessionID = gsl.GrillSessionID AND gsl.GrillSessionLogID =
(SELECT MAX(GrillSessionLog.GrillSessionLogID) FROM
GrillSessionLog, GrillSession WHERE GrillSessionLog.GrillSessionID
= GrillSession.GrillSessionID AND GrillSession.ActiveSession =
1)")
for row in cursor:
print("In for loop")
readings = readings + (row['FoodCategory'], row['FoodName'])
print("Food Cat = " + row['FoodCategory'])
cursor.close()
return readings
DB Browser 中的查询只返回一行,这是我试图在 python 代码中发生的。
解决方案
才发现问题......
使用 DB Browser,我更新了一条用于测试的记录,但未能将更改“写入”到数据库表中。结果,每次我对表执行 python 代码时,它都会使用原始记录值执行查询,因为我的更改尚未通过 DB Browser 提交。
那个大脑袋放屁....希望这将成为未来其他人的一个教训。
推荐阅读
- spring-boot - 骆驼文本文件到 sql-developer 传输 hello world
- selenium - 文本中带有撇号 (') 的元素的定位器问题
- java - 如何比较不同类型的列表
- regex - 需要正则表达式来编辑有效的电子邮件地址,但不是以某些单词开头的地址,不能使用环视
- react-native - 下载后React Native将文件路径转换为URI
- postgresql - 获得每个评分和平均值的总票数。Postgres
- amazon-web-services - AWS Cert Mgr - 如何创建根 CA 证书?
- html - 如何将列出页数的分页器下拉菜单的字体更改为更大?
- python - 第三方 API 调用后 Django 无法再找到 base.html
- sql - 根据 TypeID 创建列