Search code examples
pythonpython-3.xamazon-web-servicesbotoamazon-simpledb

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

Solution

  • 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')