integration-relational-databases-zope

advertisement
Integration of relational
databases in ZOPE
Authors: Cornel Nitu
Alin Voinea
Introduction
The Zope Object Database (ZODB) is used to store all the pages, files and other objects you create.
It is fast and requires almost no setting up or maintenance. Like a file system, it is especially good
at storing moderately-sized binary objects such as graphics.
Relational Databases work in a very different way. They are based on tables of data such as this:
Row
===
1
2
3
First Name
==========
Bob
John
Steve
Last Name
=========
McBob
Johnson
Smith
Age
===
42
24
38
Common Relational Databases for which Zope connection is
available
Here are some of the more popular database systems for which Zope connection support is
available:
Oracle
Oracle is arguably the most powerful and popular commercial relational database. It is,
however, relatively expensive and complex. Oracle can be purchased or evaluated from the
Oracle Website.
DB2
DB2 from IBM is the main commercial competitor to Oracle. It has similar power but also
similar expense and complexity. More information from
http://www.ibm.com/software/data/db2/
PostgreSQL
PostgreSQL is a leading open source relational database with good support for SQL standards.
You can find more information about PostgreSQL at the PostgreSQL web site.
MySQL
MySQL is a fast open source relational database. You can find more information about
MySQL at http://www.mysql.com/
SAP DB
An open source database developed by SAP. Has an Oracle 7 compatibility mode. More
information and downloads from http://www.sapdb.org/.
Sybase
Sybase is another popular commercial relational database. Sybase can be purchased or
evaluated from the Sybase Website.
SQL Sever
Microsoft's full featured SQL Server for the Windows operating systems. For any serious use
on Windows, it is preferable to Microsoft Access. Information from
http://www.microsoft.com/sql/
Interbase
Interbase is an open source relational database from Borland/Inprise. You can find more
information about Interbase at theBorland web site. You may also be interested in FireBird
which is a community maintained offshoot of Interbase. The Zope Interbase adapter is
maintained by Zope community member Bob Tierney.
Gadfly
Gadfly is a relational database written in Python by Aaron Waters. Gadfly is included with
Zope for demonstration purposes and small data sets. Gadfly is fast, but is not intended for
large amounts of information since it reads its entire data set into memory. You can find out
more about Gadfly at http://gadfly.sourceforge.net/.
For more information about connecting this databases with ZOPE see
http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition/RelationalDatabases.stx
Solution #1 MS Access
Microsoft Office Access, previously known as Microsoft Access, is a relational database
management system from Microsoft which combines the relational Microsoft Jet Database Engine
with a graphical user interface and software development tools.
To use MS Access with ZOPE you will need a commercial database adapter product (mxODBC)
and a ZOPE adapter (mxODBC Zope Database Adapter). These adapters can be downloaded
from the http://www.egenix.com/products website. The installation instructions are included in the
packages.
To define a new data source type add this section to /etc/odbcinst.ini (the odbcinst.ini configuration
file may be in a different location on your machine):
[MDBToolsODBC]
Description = MDB Tools ODBC drivers
Driver
= /usr/lib/libmdbodbc.so.0
Setup
=
FileUsage
=1
CPTimeout
=
CPReuse
=
UsageCount
=4
Note that the libtdsodbc.so file may be located in a different directory on your machine.
Finally, define a new database connection in the /etc/odbc.ini file (the odbc.ini configuration file
may be in a different location on your machine):
[example_test]
Description = Microsoft Access Database
Driver = MDBToolsODBC
Database = /tmp/example.mdb
Servername = localhost
UserName =
Password =
port = 5432
Use ‘isql example_test’ to test your connection. If the message is ‘Connected!’ everything should
be ok. Now, start the ZOPE server, login into ZMI and add a new eGenix mxODBC Database
Connection
And test your connection by executing a simple query:
If everything went well, you should create Z SQL Methods in order to interrogate your database
(see example in the next section).
Another way (cost free) would be to convert your MS Access database structure and data to
MySQL format using MDB Tools.
First download and install the MDB Tools package from http://mdbtools.sourceforge.net/ and
checkout the ‘mdb2mysql’ script from EEA's Subversion repository (https://svn.eionet.europa.eu/)
Now use the mdb2mysql script to generate a MySQL dump from the MS Access database:
| mdb2mysql /tmp/example.mdb > example.sql
Run MySQL, create a new database and import the example.sql file. Instructions how to hook Zope
up to a MySQL database you will find in the next chapter.
2. MySQL
MySQL, the most popular Open Source SQL database management system, is developed,
distributed, and supported by MySQL AB. MySQL AB is a commercial company, founded by the
MySQL developers. It is a second generation Open Source company that unites Open Source values
and methodology with a successful business model.

To use MySQL with ZOPE you'll need a database adapter product called ZMySQLDA
(Zope MySQL Database Adapter).

The latest stable release can be downloaded from
http://www.zope.org/Members/adustman/Products/ZMySQLDA

ZMySQLDA depends on:

mysql

mysql-devel

mysql-python (http://sourceforge.net/projects/mysql-python)
Installation
Installing mysql, mysql-devel

Fedora:

Debian:
yum install mysql mysql-devel
apt-get install mysql mysql-devel
Installing mysql-python
1. Download latest stable release from http://sourceforge.net/projects/mysql-python
2. Untar, unzip package
3. Run:
<path/to/python/used/by/zope> setup.py install
Installing ZmySQLDA
1. Download latest stable release from
http://www.zope.org/Members/adustman/Products/ZMySQLDA
2. Untar, unzip
3. Copy ZmySQLDA into your Zope Products directory
4. Run:
<path/to/python/used/by/zope> setup.py install
5. Restart zope
Zope connect and test

To add/edit databases, users, tables, grant access see
http://dev.mysql.com/doc/refman/5.0/en/index.html

Login into ZMI and add a Z MySQL Database Connection

To browse available tables see Browse Tab

To test connection see the Test Tab
Z SQL Methods
Z SQL Methods are Zope objects that execute SQL code through a Database Connection. All Z SQL
Methods must be associated with a Database Connection. Z SQL Methods can both query and
change database data. Z SQL Methods can also contain more than one SQL command.
A ZSQL Method has two functions: it generates SQL to send to the database and it converts the
response from the database into an object.
Examples of ZSQL Methods
Create a new Z SQL Method called view that returns title and description from the chm table,
filtered and sorted by title.
SELECT title, description FROM chm WHERE
title LIKE <dtml-sqlvar title type="string">
ORDER BY title
Using ZSQL Methods

Add a Page Template named index and insert the following code:
<tal:block define="title request/title|string:">
<!-- Search -->
<form action="index">
<input type="text" name="title" tal:attributes="value title" />
<input type="submit" name="submit" value="GO" />
</form>
<!-- Results -->
<table>
<tr>
<th i18n:translate="">Title</th>
<th i18n:translate="">Description</th>
</tr>
<tr tal:repeat="item python:here.view(title=title).dictionaries()">
<td tal:content="item/title" />
<td tal:content="item/description" />
</tr>
</table>
</tal:block>
Download