python - 如何在web.py/python中检查sql查询结果是否为空

标签 python mysql web.py

我正在 web.py 框架中开发一个 Web 应用程序,需要一种方法让 web.py/python 检查 sql 查询的结果是否为空。

这是我当前的功能:

def get_hours():
    result = dbconn.query("select * from hours where date < (select max(date) from last_export) order by date DESC")
    return result

这按预期工作,但如果查询结果为空,我希望函数返回 False。我已经知道,如果没有 count for 循环,python 无法返回可迭代对象中有多少个元素(由 dbconn.query 返回,无论它是否为空)。这对我来说是行不通的,因为我不希望结果在返回之前被迭代。

这是我想用该函数实现的示例:

def get_hours():
    result = dbconn.query("select * from hours where date < (select max(date) from last_export) order by date DESC")

    if <result is empty/no rows/nothing to iterate>:
        return False

    else:
        return result

有什么建议吗?

最佳答案

def get_hours():
    result = dbconn.query("select * from hours where date < (select max(date) from last_export) order by date DESC")   

    if result:
        return result
    else:
        return False

这里有一个非常有趣的答案,以了解更多详细信息:

https://stackoverflow.com/a/2710949/492258

关于python - 如何在web.py/python中检查sql查询结果是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8694653/

相关文章:

python - 在 web.py 中获取自定义 HTTP 请求 header ?

python - web.py 中的超链接

python - 如何调整滑动窗口滑动多远(step)

python - GridSearchCV:评分不使用选择的 XGBRegressor 评分方法

php - Laravel 5 动态获取巨大的 mysql 数据到 jquery-datatable

php - 使用连接并将连接结果放入数组中?

python - Django DLL加载失败: The specified module could not be found

python - 在 Python OpenCV 中查找 connectedComponents 的颜色

mysql - 如何在 MySQL 中显示表的唯一约束?

python - webpy : how to override content of basic template?