2011/10/5 Bikes At Work Inc. <[email protected]>:
> I would like to select the first row of a query from a table.  This will work 
> if the query returns valid data:
>
> result = db.select('tablename', where='id=%s' % id)[0]
>
> but throws an IndexError if the query returns an empty dataset.
>
> I can do this:
> try:
>        result = db.select('tablename', where='id=%s' % id)[0]
>        (...do stuff with result...)
> except IndexError:
>        (...handle error...)
>
> but that seems a little inelegant.
>
> I also tried this:
>
> result = db.select('tablename', where='id=%s' % id)
> if len(result.list()) > 0:
>        (...do stuff with result...)
>
> but that destroys the first iterator so I can't use the result.
>
> Is there a better way to handle this?  Ideally, I would like db.select() to 
> just return "None" if there is no result :)

You can just do this:

if result:
    ....

-- 
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/webpy?hl=en.

Reply via email to