[Zope3-Users] Any document for use ZopeDatabaseAdapter?
TAHARA Yusei
yusei at domen.cx
Sat Oct 29 10:44:05 EDT 2005
Hi.
At Fri, 28 Oct 2005 20:43:37 +1000,
Simon Hang wrote:
> Hi,
>
> Is there any document or examples for ZopeDatabaseAdapter?
>
> I need to do some sql query in my content object.
I don't know tutorials or examples, but you can use it simply like a
python dbi module.
First you need to setup DA and register it as a utility in
sitemanager, then you can get DA using by zapi.getUtility.
DA interface is very similar to python dbi module. I think zope3
becomes easier than zope2 to make rdb application.
from zope.app import zapi
from zope.app.rdb.interfaces import IZopeDatabaseAdapter
from zope.app.rdb import sqlquote
from zope.app.sqlscript.dtml import SQLDTML
class TableView:
def cursor(self):
da = zapi.getUtility(IZopeDatabaseAdapter, 'gadfly', self.context)
con = da()
return con.cursor()
def getdata(self):
cursor = self.cursor()
cursor.execute('select name from mytable')
return cursor.fetchall()
def insertdata(self, name):
cursor = self.cursor()
template = SQLDTML('insert into mytable values(<dtml-sqlvar name type=string>)')
cursor.execute(template(name=name))
self.request.response.redirect('@@list.html')
Best Regards,
--
Tahara Yusei
yusei at domen.cx
More information about the Zope3-users
mailing list