我正在 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
这里有一个非常有趣的答案,以了解更多详细信息:
关于python - 如何在web.py/python中检查sql查询结果是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8694653/