[Zope] Calling SQL from Python products

Johan Carlsson johanc at easypublisher.com
Tue Sep 21 11:46:59 EDT 2004


Here an example of a really fast way of executing SQL questions in a 
Python Product:

from Shared.DC.ZRDB.Results import Results
from Shared.DC.ZRDB import RDB

class MyProduct(SimpleItem):

    def __init__(self, database_name='MySQL_database_connection'):
        self.database_name=database_name

    def getDB(self):
        return getattr(self, self.database_name, None)
   
    def queryDB(self, query):
        """query is a text with your SQL quetion, note not ZSQLMethod 
code!"""
        db=self.getDB()
        if db is not None:
            dbc=db()      #get our connection
            res=dbc.query(query)
            if type(res) is type(''):
                f=StringIO()
                f.write(res)
                f.seek(0)
                result=RDB.File(f)
            else:
                result=Results(res)  
        return result 

Cheers,
Johan


More information about the Zope mailing list