[Tutor] Building an SQL query

Roger Merchberger zmerch at 30below.com
Fri Jun 3 00:20:51 CEST 2005


Rumor has it that Alan G may have mentioned these words:

>Hmm, I dunno ADOpy but assume it somehow miraculously turns your data
>set into a dictionary of some sort?

I dunno ADOpy, but the pg module for PostgreSQL can return a list of 
dictionaries from a query.

 >>> import pg
 >>> pg.set_defuser('example')
 >>> pg.set_defpasswd('example')
 >>> pg.set_defbase('spam')
 >>> pgpasswd = pg.DB()

 >>> pgpasswd.query('select dtg, classc, helo from ips limit 3;').dictresult()

[{'helo': 'veda.cz', 'dtg': '2005-03-30', 'classc': '85.39.122'},
  {'helo': 'ck336290-a.dokku1.fr.home.nl', 'dtg': '2005-03-30', 'classc': 
'217.123.211'},
  {'helo': 'keymaster.com', 'dtg': '2005-03-30', 'classc': '220.73.88'}]

*** output edited slightly with carriage returns ***

>  How it guesses which order the
>SELECT will return the fields is a mystery to me,

It's a mystery to some RDBs as well, IIRC with SQL there's no 'default 
behavior' -- if it's not defined, it can be spit out in any order it 
chooses; prolly depends on the implementation.

It might also matter how the indices & primary keys are set up as to what 
comes out first...

>  but maybe it has
>knowledge of the Postgres hashing function or somesuch.

With dictionaries, it doesn't matter nearly so much. ;-)

The ease with which data can be I/O'd thru PostgreSQL with Python is one of 
the main factors of my dumping Perl for it; I can spend more time diddling 
with the data than I need to do making the proggies run.
;-)

HTH,
Roger "Merch" Merchberger

--
Roger "Merch" Merchberger   | "Bugs of a feather flock together."
sysadmin, Iceberg Computers |           Russell Nelson
zmerch at 30below.com          |



More information about the Tutor mailing list