I'm working on a web application in the web.py framework and need a way for web.py/python to check if the result of a sql query is empty.
Here's my current function:
def get_hours():
result = dbconn.query("select * from hours where date < (select max(date) from last_export) order by date DESC")
return result
This works as expected, but I want the function to return False if the result of the query is empty. I already know that python has no way of returning how many elements there is in a iterable object(which is returned by the dbconn.query no matter if it's empty or not), without a count for loop. Which to me wouldn't work since i don't want the result to be iterated BEFORE it's returned.
Here's an example of what I want to achieve with the function:
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
Any suggestions?
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
Here is very interesting answer for further details: