[DB-SIG] return value of .fetchall() with an empty result set

M.-A. Lemburg mal@lemburg.com
Thu, 25 Apr 2002 14:22:54 +0200

brian zimmer wrote:
> I need a clarification on the use of .fetchall().  Which of the below
> scenarios is correct?
> The call
>  c.execute("select * from some_table where 1=0")
>  data = c.fetchall()
>  (1) raises an Error
>  (2) data == []

This one.

>  (3) data is None
> Under some cases I can not know the number of rows returned in the query
> so the user must try one of the fetchXXX methods.  I currently implement
> zxJDBC as (3) which is probably the most incorrect.  I *think* it should
> probably return (2) to be more consistent with .fetchmany().  (1) seems
> rather harsh to me as this is not really an exceptional case, there's
> just no data.  Either way it appears I'm not consistent with the API.
> How have others implemented this? 


> Is everyone else raising an Error?


> Is the preferred method to check the .rowcount attribute and only call
> .fetchXXX if it's greater than zero?

No. .fetchall() should return an empty list which nicely maps
to an empty result set.

Marc-Andre Lemburg
CEO eGenix.com Software GmbH
Company & Consulting:                           http://www.egenix.com/
Python Software:                   http://www.egenix.com/files/python/