[DB-SIG] Use of record sets

Dittmar, Daniel daniel.dittmar@sap.com
Wed, 4 Apr 2001 18:11:37 +0200


> result = curs.fetchone() 
> print "Found id '%s', first name '%s' and last name '%s'"%( result[0],
result[1], result[2] ) 

vs.

> print "Found id '%s', first name '%s' and last name '%s'"%( result['id'],
result['first_name'], 
>   result['last_name'] ) 

How about 

id, first_name, last_name = curs.fetchone ()
print "Found id '%s', first name '%s' and last name '%s'"%( id, first_name,
last_name)

or better still 

for id, first_name, last_name in curs:
    print "Found id '%s', first name '%s' and last name '%s'"%( id,
first_name, last_name)

This is of couse not directly an argument against using strings as a column
index (DBI has them, JDBC has them). 

There is one annoying property of SQL in this context: identifier are
converted to uppercase unless quoted. Should the UserDict mimic this
behaviour by trying both the actual string and the uppercase version?

And how about returning row objects. That would allow .dot notation (if the
column names are valid Python identifier):
print "Found id '%s', first name '%s' and last name '%s'"%( result.id,
result.first_name, result.last_name)

My personal preference: 'for id, first_name, last_name in curs:'

Daniel

--
Daniel Dittmar
daniel.dittmar@sap.com
SAP DB, SAP Labs Berlin
http://www.sapdb.org/