首页 > 解决方案 > 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 代码中发生的。

标签: pythonsqlite

解决方案


才发现问题......

使用 DB Browser,我更新了一条用于测试的记录,但未能将更改“写入”到数据库表中。结果,每次我对表执行 python 代码时,它都会使用原始记录值执行查询,因为我的更改尚未通过 DB Browser 提交。

那个大脑袋放屁....希望这将成为未来其他人的一个教训。


推荐阅读