How to test where a sqllite3 returns results or empty in Python?

Viewed 43
cursor = self.conn.execute(query)

        for row in cursor:
            print("song:", row[0])

My goal is not to return query result, but to see whether the query return value or not. I don't see there is a way to test on the 'cursor' object. Ideally, I would want to see something like:

This is in Python.

 if cursor is Empty(None):
      print("no result")
  else:
     print("result founds")

Is there a way to do that?

1 Answers

This is usually achieved by using the EXIST sql statement. Assuming you are using sqlalchemy, this link from the doc or this post may be helpful

The easiest is probably to wrap your query with

s = select([table.c.col1]).where(table.c.col2==5)
query = exists(s)
result = conn.execute(query).fetchone()
return True if result is not None else False
Related