[DB-SIG] Re: DB-SIG digest, Vol 1 #511 - 4 msgs
Billy G. Allie
Billy G. Allie" <Bill.Allie@mug.org
Sat, 03 Nov 2001 13:20:42 -0500
--==_Exmh_-1873560944P
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
David Bolen <db3l@fitlinxx.com> writes:
> Gary Maynard [maynard@mpi-cbg.de] writes:
> =
> > 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']
> =
> Others have pointed out some of the externally available modules
> (e.g., dtuple), but it's also pretty simple to just write your own
> function to do it. Here's a sample of a function that is given
> a cursor with a command previously executed, and automatically
> processes the fetchall() into a list of dictionaries. Far less
> generic than something like dtuple, but it does meet your above goals:
Another possibility is to use the PgResultSet object from pyPgSQL =
(http://pypgsql.sourceforge.net). This object acts as a list or a dictio=
nary. =
It also exposes the result columns as attributes by column name, making =
it =
possible to reference columns using:
given: result =3D cursor.fetchall("select * from Address")
1. result[6][2] /* accessed as a list */
2. result[6]['street'] /* accessed as a dictionary */
3. result[6].street /* accessed as an attribute */
-- =
____ | Billy G. Allie | Domain....: Bill.Allie@mug.org
| /| | 7436 Hartwell | MSN.......: B_G_Allie@email.msn.com
|-/-|----- | Dearborn, MI 48126|
|/ |LLIE | (313) 582-1540 |
--==_Exmh_-1873560944P
Content-Type: application/pgp-signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
David Bolen <db3l@fitlinxx.com> writes:
> Gary Maynard [maynard@mpi-cbg.de] writes:
> =
> > 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']
> =
> Others have pointed out some of the externally available modules
> (e.g., dtuple), but it's also pretty simple to just write your own
> function to do it. Here's a sample of a function that is given
> a cursor with a command previously executed, and automatically
> processes the fetchall() into a list of dictionaries. Far less
> generic than something like dtuple, but it does meet your above goals:
Another possibility is to use the PgResultSet object from pyPgSQL =
(http://pypgsql.sourceforge.net). This object acts as a list or a dictio=
nary. =
It also exposes the result columns as attributes by column name, making =
it =
possible to reference columns using:
given: result =3D cursor.fetchall("select * from Address")
1. result[6][2] /* accessed as a list */
2. result[6]['street'] /* accessed as a dictionary */
3. result[6].street /* accessed as an attribute */
- -- =
____ | Billy G. Allie | Domain....: Bill.Allie@mug.org
| /| | 7436 Hartwell | MSN.......: B_G_Allie@email.msn.com
|-/-|----- | Dearborn, MI 48126|
|/ |LLIE | (313) 582-1540 |
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.2 (UnixWare)
Comment: Exmh version 2.2 06/23/2000
iD8DBQE75DV6nmIkMXoVVdURAgj1AKC/xm2exi2F59FcTip4Zrx7/wpkwQCg1nin
ZzU+wOrn8LuIqcM8wVLHSIg=
=I8uG
-----END PGP SIGNATURE-----
--==_Exmh_-1873560944P--