[Zope3-Users] Re: Newcomer to Zope, question about Postgres

Andreas Elvers andreas at work.de
Wed Dec 14 07:20:42 EST 2005


Rakesh Malik wrote:
> Is it necessary to use Postgres in Zope3? I'm running into all sorts of 
> trouble
> getting sqlos to build, like this latest one:
> IOError: [Errno 2] No such file or directory: 
> '/home/rmalik/Zope3/zopeskel/lib/ftesting.zcml'make: *** [test] Error 1
> 
> The SQLObject imports fine now, at least.

As I said, it is not easy to get sqlos running. And if you feel uneasy, 
consider
not using it. As Stephan points out, you don't need sqlos to access 
postgres
from within zope. All you need is the psycopgda adapter. This is a zope3
package that needs to be installed (you'd need it for sqlos too).

It's available through svn with:
svn co svn://svn.zope.org/repos/main/psycopgda/trunk

put it in yourinstance/lib/python
copy psycopgda-configure.zcml into yourinstance/etc/package-includes
restart zope.

Now to add a database connection go to the management interfaces,
inside the top folder you will find the SiteManager ++etc++site. In
its 'default' SiteManagementFolder you will be able to add a Psycopg DA.
Don't forget to click 'Register'. This will activate the Psycopg utility.
 From the newly created connection you are able to test the connection.
Don't forget to click 'Register'.

Now you have a connection to your database.

Since all programming is done in the filesystem you will have to create
some content component  that actually queries the database utility.

You could add a view to your content component with a method
'getEmployee'.

something like this (not tested at all and incomplete example)
To understand this you should be familiar with the message board
example in Stephans zope3 dev book.

from zope.app import zapi
from zope.app.rdb.interfaces import IZopeDatabaseAdapter
from zope.app.rdb.interfaces import IZopeCursor,IZopeConnection
class EmployeeDetails(object):
    def getEmployee(self,id):
       # this will get you the utility
       dbUtil = 
zapi.queryUtility(IZopeDatabaseConnection,'NameOfYourDBUtility')
       # To get a connection call the adapter and adapt to IZopeConnection
       connection = IZopeConnection(dbUtil())
       # Now get the cursor and adapt it to IZopeCursor
       cur = IZopeCursor(connection.cursor())
	
       cur.execute('select * from employee where id=%d order by 
something' % id)
       while cur.dictfetchone():
           ... and so on ...

No need for sqlos :-)

- Andreas




More information about the Zope3-users mailing list