【发布时间】:2012-01-01 18:17:40
【问题描述】:
我正在开发 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。我已经知道 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
有什么建议吗?
【问题讨论】: