[DB-SIG] There is a better way

M.-A. Lemburg mal@lemburg.com
Wed, 12 Jun 2002 10:29:43 +0200


Richard Taylor wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> On Wednesday 12 June 2002 7:22 am, Stuart Bishop wrote:
> 
>>On Friday, June 7, 2002, at 08:34 PM, Richard Taylor wrote:
>>
>>>        if cr.description == None or cr.rowcount == 0 :
>>>            return []
>>>
>>>        try:
>>>            res = cr.fetchall()
>>>
>>>
>>>I would welcome comments on whether this will work on all DB API
>>>modules.
>>
>>Unfortunatly, rowcount may be undefined until all results have been
>>retrieved (otherwise the DB may have to execute some massive query
>>twice -
>>once to determine the rowcount and once to actually retrieve the rows -
>>in cases where the result cannot be cached in memory).
> 
> 
> I am not sure that it matters. It would only matter if the rowcount was set to 
> zero when there really was some rows returned. That would appear to me to be 
> unlikely. If it is possible that cr.rowcount is not defined at all then I 
> guess the conditional could be:
> 
> if cr.description == None or (hasattr(cr,'rowcount') and cr.rowcount == 0):
> 	return []
> 
> This would assume that if a table description is set and rowcount is not yet 
> defined some rows have been returned from the database. Or at least that a 
> call to fetchXXX will not raise an exception.
> 
> Any of the db module maintainers want to comment on this? I believe that 
> psycopg would obey this condition because rowcount appears to be set to zero 
> when zero rows are returned. 

The .rowcount variable must be set to -1 if it is unknown for
some reason, e.g. if no query has been executed on the cursor
of the database does not have the information available.

A .rowcount of 0 always indicates that the result set is
empty. Unfortunately, some database drivers and interfaces
are broken in this respect (including some ODBC drivers
out there), so applications should be aware of this.

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Company & Consulting:                           http://www.egenix.com/
Python Software:                   http://www.egenix.com/files/python/
Meet us at EuroPython 2002:                 http://www.europython.org/