[DB-SIG] fetchone -> dictionary?
Wed, 20 Jan 1999 16:59:57 -0800
Stephen Ng wrote:
> I'm just getting started with this stuff, so sorry if this is a newbie question/suggestion.
> Why not have a flavor of fetchone() which returns a dictionary instead a sequence? Then we could write:
> row = dfetchone()
> print row['FieldName']
> instead of
> print row[SomeConstantWhichMightChange]
> Perhaps dfetchall could be implemented as an array of dictionaries?
Suggestions like these have come up in the past. The reason that a
simple tuple is returned is that it is much easier for the module
implementor. Rather than requiring all module implementors to deal with
more complex data structures, we rely on Python itself to do the work.
You can write a simple layer over the cursor object that fills in a
dictionary based on the cursor.description results plus the returned
tuple. If you check the archives, you'll find a solution that I posted
called "dtuple.py" that can wrap a tuple into a lightweight object
allowing "row.FieldName" types of access. It may also allow the
dict-type access that you describe, but I've forgotton.
For example, most implementations of fetchone() access a set of buffers
corresponding to the columns that were fetched. The names of those
columns, typically, are not readily available at that point in time.
Further, the columns might actually represent *functions* or other
complex situations. Having to reference those via a string would be
*very* difficult. Is it case sensitive? What about spaces? Quoting? etc.
Greg Stein, http://www.lyra.org/