SQLite查询难度

时间:2019-09-06 17:15:56

标签: python sqlite

我有一个带有三个关系表的SQLite数据库。我正在尝试根据ID关系从日志表中返回最大记录以及其他表中的相关列。

我在数据库浏览器中创建了查询,并验证了它返回了预期的记录,但是,当我在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浏览器中的查询仅返回一行,这是我想要在python代码中发生的行。

1 个答案:

答案 0 :(得分:0)

刚刚发现了问题。...

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

在那上面放着大脑袋。...希望将来这对其他人来说是一个教训。

相关问题