[Zope] Using Zope DB connection from a Python class

Monty Taylor mtaylor@goldridge.net
Fri, 23 Jun 2000 10:24:23 +0200


This is a multi-part message in MIME format.
--------------1256C90F4BAC7F76823847D8
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

I was also trying to accomplish this a while back. I managed to add the
new class with a database connection and execute sql statements. My
class had some stuff like this:

        def __init__(self,id,title,dbconn):
                self.id=id
                self.title=title
                self.dbconn=dbconn
                self.times={}

        def getProducts(self):
            stmt = 'select prdt_id as id, product as name from products'
            dummy, components = self.exec_sql(stmt)
            return components

        def exec_sql(self, stmt):
          "Execute a piece of SQL code -- Need to convert to some sort
of ZSQL Method"
          db = getattr(self, self.dbconn)
          col_names, raw_results = db().query(stmt)
          col_names = map(lambda x: x['name'],col_names)
          
          return col_names, raw_results

Now it worked... but here's my problem/question... These methods, while
executing SQL, are not ZSQL Methods, which means I'm not getting any of
the good caching and other bollocks. I was categorically unsuccessful
adding ZSQL Methods to the class. Does anyone have any tips to how I
could turn exec_sql into a ZSQL Method?

Monty

Ivan Raikov wrote:
> 
> Greetings,
> 
> For the past few days, I've been unsuccessfully trying to use a Zope
> ODBC connection from within a Python class.
> 
> I have the following situation:
> 
> 1. A Python class, Order, which has this method:
> 
>     def create (self, cust_id, connection_id = None, itemcode = None,
>                 quantity = 0):
> 
>         while self is not None:
>             if hasattr(self, 'objectValues'):
>                 for o in self.objectValues():
>                     if hasattr(o, 'id'):
>                         if o.id == connection_id:
>                             self.connection = o
> 
>         self.cust_id = cust_id
>         self.itemcode = itemcode
>         self.quantity = quantity
>         query = 'insert into orders (cust_id, itemcode, quantity) values (%s, \'%s\', %s)' % (self.cust_id, self.itemcode, self.quantity)
> 
>         if self.connection != None:
>             self.connection._begin()
>             result = self.connection.query (query)
>             print result
>             self.connection._finish()
> 
> 2. A Zope class, TestOrder, which subclasses Order. Its constructor
>    tries to invokes 'create', however upon executing the SQL query,
>    Zope  either dies and is restarted, or just sits there, doing
>    nothing, until the HTTP connection times out. In either case, the
>    new record is not written in the database.
> 
> 3.      Zope 2.1.7
>         Z ODBC DA 3.0.3
>         Solid dev kit 2.3
>         RedHat Linux 6.1 on i386
> 
> Please help.
> 
> Thanks,
> Ivan Raikov
> 
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )
--------------1256C90F4BAC7F76823847D8
Content-Type: text/x-vcard; charset=us-ascii;
 name="mtaylor.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Monty Taylor
Content-Disposition: attachment;
 filename="mtaylor.vcf"

begin:vcard 
n:Taylor;Monty
tel;cell:+31 (0)6 200 17486
tel;fax:+31 (0)20 589 5566
tel;work:+31 (0)20 589 5517
x-mozilla-html:TRUE
url:http://www.goldridge.net
org:Information Innovation
adr:;;Amstelveenseweg 88-90;Amsterdam;;1075 XJ;The Netherlands
version:2.1
email;internet:mtaylor@goldridge.net
title:Information Artist
x-mozilla-cpt:;0
fn:Monty Taylor
end:vcard

--------------1256C90F4BAC7F76823847D8--