[egenix-users] Converting ZQL method return value to dictionary

M.-A. Lemburg mal at lemburg.com
Sun Aug 10 14:52:33 CEST 2003


Wade Leftwich wrote:
>> Now to my questions:
>> 1) the class "r", is it a Zope thingy or a mxODBC thingy?
>> 2) is there a mothod to do what I need?
>>
>>
>> Than you for your time...
>> Jerry
>>
>> P.S.  There are other dictionary functions not support by this "R"
>> instance that I would like to use also...
> 
> In the past I have frequently written a function to convert each tuple
> in a zsql result to a dictionary. But looking at the current docs
> 
> http://zope.org/Documentation/Books/ZopeBook/current/RelationalDatabases.stx 
> 
> toward the bottom of the page, I see that zsql results have a 
> dictionaries() method that returns a list of dicts. I wonder if that's a 
> recent addition, or if I just never noticed it before?

It's been in Zope for quite a while (and I must have forgotten
all about it too :-). It gives you a list of dictionaries, one
for each row, mapping column names to values.

> If that does not work for you, try this:
> 
> def row2dict(row, names):
>     D = {}
>     for i in range(len(names)):
>         D[names[i]] = row[i]
>     return D
> 
> result = context.somezsqlmethod()
> for row in result:
>     print row2dict(row, result.names())

That's pretty much what result.dictionaries() does for the
complete result set :-)

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Software directly from the Source  (#1, Aug 10 2003)
 >>> Python/Zope Products & Consulting ...         http://www.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________



More information about the egenix-users mailing list