Instabrite
Instabrite

Reputation: 2299

Checking Python Boto SimpleDB for empty result set

How can I check to see if the result set is empty when querying SimpleDB in Boto 2? Can I check it before it goes into the for loop?

rs = dom.select(query)

for j in rs:
  ...do something

Upvotes: 1

Views: 643

Answers (2)

franklinsijo
franklinsijo

Reputation: 18280

The ResultSet returned is an iterator. You can check whether it has any value with next() which fetches the first element from the cursor if there are any. If it is empty, it raises StopIteration error.

rs = dom.select(query)

try:
    rs.next()
except StopIteration:
    print('Empty ResultSet')

Upvotes: 1

Darek
Darek

Reputation: 2921

rs is a Python object, can you just do this?

rs = dom.select(query)
if len(rs) > 0:
    for j in rs:
      ...do something
else:
    print("resultset is empty")

Upvotes: 0

Related Questions