[Zope] how to store ZSQL query results in a python var

2005-04-07 Thread 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 :
 (lt;Shared.DC.ZRDB.Results.Results instance at 0x41dd966cgt;)

please help.
-- 
Share the vision of difference with ME
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] how to store ZSQL query results in a python var

2005-04-07 Thread Andreas Jung

--On Donnerstag, 7. April 2005 4:10 Uhr -0700 prabuddha ray 
[EMAIL PROTECTED] 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 :
 (lt;Shared.DC.ZRDB.Results.Results instance at 0x41dd966cgt;)
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

pgpNpZ3Jmigbo.pgp
Description: PGP signature
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] how to store ZSQL query results in a python var

2005-04-07 Thread Tino Wildenhain
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 :
  (lt;Shared.DC.ZRDB.Results.Results instance at 0x41dd966cgt;)
 
 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 

___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )