how to store ZSQL query results in a python var
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. -- Share the vision of difference with ME
--On Donnerstag, 7. April 2005 4:10 Uhr -0700 prabuddha ray <buddharay@gmail.com> wrote:
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>)
The RDBMS chapter of the Zope Book that call a ZSQL method returns something that behaves like a list of rows. That's what you see when you look at the output. Accessing and working with ZSQL methods is carefully described in the Zope Book (2.7 edition) - isn't it? -aj
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
participants (3)
-
Andreas Jung -
prabuddha ray -
Tino Wildenhain