Checking Python Boto SimpleDB for empty result set

622 Views Asked by At

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
2

There are 2 best solutions below

0
franklinsijo On BEST ANSWER

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')
1
Darek On

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")