[DB-SIG] Python database API 2.0/returning Column names with data

Gary Maynard maynard@mpi-cbg.de
Fri, 2 Nov 2001 11:34:52 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_0044_01C16392.63620DB0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

HI,
  We are trying to implement a method whereby we can return data from =
the database in key value dictionary pairs.

so for example we have a table something like this:

Table: Address
Name varchar(20),
Street varchar(20),
Town varchar(20),
ccode char(2)

and we want to return the data into a dictionary so that we also have =
access to the column title.  For example, let's say we query the =
database with something like this:

result =3D cursor.fetchall("select * from Address")

Then we'd like to put the data from result into another data object, =
call it sorted_result.  We'd like to include the column names in this =
object such that we could obtain any piece of data by giving it the row =
number and column name.  So, if we wanted to obtain the Street of the =
6th row, we would say something like:

sorted_result [6]['Street']

And if we wanted to iterate over all the streets we can do:

for x in result:
   do stuff with result[x]['Street']

There seems to be fairly standard ways of doing this in DBI =
(selectrow_hashref) and ODBC (Datahash), but I can't seem to make it =
work with this API.

Any suggestions?
thanks
Gary

------=_NextPart_000_0044_01C16392.63620DB0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2920.0" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>HI,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; We are trying to implement a =
method whereby=20
we can return data from the database in key value dictionary =
pairs.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>so for example we have a table =
something like=20
this:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Table: Address</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Name varchar(20),</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Street varchar(20),</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Town varchar(20),</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>ccode char(2)</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>and we want to return the data into a =
dictionary so=20
that we also have access to the column title.&nbsp; For example, let's =
say we=20
query the database with something like this:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>result =3D cursor.fetchall("select * =
from=20
Address")</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Then we'd like to put the data from =
result into=20
another data object, call it sorted_result.&nbsp; We'd like to include =
the=20
column names in this object such that we could obtain any piece of data =
by=20
giving it the row number and column name.&nbsp; So, if we wanted to =
obtain the=20
Street of the 6th row, we would say something like:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>sorted_result =
[6]['Street']</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>And if we wanted to iterate over all =
the streets we=20
can do:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>for x&nbsp;in result:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp; do stuff with=20
result[x]['Street']</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>There seems to be fairly standard ways =
of doing=20
this in DBI (selectrow_hashref) and ODBC (Datahash), but I can't seem to =
make it=20
work with this API.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Any suggestions?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>thanks</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Gary</FONT></DIV></BODY></HTML>

------=_NextPart_000_0044_01C16392.63620DB0--