[Zope] how to store ZSQL query results in a python var
Tino Wildenhain
tino at wildenhain.de
Thu Apr 7 07:27:22 EDT 2005
Am Donnerstag, den 07.04.2005, 04:10 -0700 schrieb prabuddha ray:
> from a newbie,
> i've this ZSQLmethod returning me a string only. how do i
> store it a var in my python script.
> eg., userlevel = container.getUserLevel(uname=user)
>
> when i print it using html_quote as :
> print "(%s)" % html_quote(userlevel)
> return printed
> output is :
> (<Shared.DC.ZRDB.Results.Results instance at 0x41dd966c>)
>
> please help.
Well, ZSQL Methods always return result sets, never strings.
The restult set is an object which works similar to a list
with result objects, each result object having attributes
which correspond to column names of your query.
The result objects have a method dictionaries() which in fact
return the data as regular list with dictionaries for the
rows (easier to look at if you see their string representation)
so what you probably want goes like this:
result=container.getUserLevel(uname=user)
if result:
userlevel=result[0].userlevel
else:
userlevel=somedefault # or raise error or whatnot
the "if result:" idiom handles the fact the query might not
return any result. It is true however if you get at least one
row. result[0] addresses the very first row of the result
- which must exist in a non empty result set.
HTH
Tino
More information about the Zope
mailing list