(eBook - pdf) Zope and MySQL.pdf

(129 KB) Pobierz
Zope And MySQL
By icarus
This article copyright Melonfire 2000−2002. All rights reserved.
99234170.004.png 99234170.005.png
Zope And MySQL
Table of Contents
Kicking The Tyres. ..............................................................................................................................................1
Making The Connection. ....................................................................................................................................2
Adapting To The Environmen. .........................................................................................................................3
The Magic Of The Movies. .................................................................................................................................5
Visiting The Box Office ......................................................................................................................................7
Adding Things Up. ..............................................................................................................................................9
Submitting To The King. ..................................................................................................................................12
Erasing The Past. ..............................................................................................................................................14
Of Methods And Madness. ...............................................................................................................................16
An Object Lesson. .............................................................................................................................................21
i
99234170.006.png
Kicking The Tyres
As a recent convert to Zope from PHP, I was curious: could Zope really give me all the capabilities I was used
to in PHP?
I decided to find out, by using one of my most common activities with PHP as the benchmark: connecting to
and retrieving records from a MySQL database. As it turned out, Zope manages this quite well, and it's also
nowhere near as complicated as you might think. And so, over the course of this article, I'm going to show
you how to hook your Zope server up to a MySQL database, and write DTML code that allows you to retrieve
and manipulate MySQL table records using standard SQL commands. If this sounds interesting, keep reading.
Kicking The Tyres
1
99234170.007.png
Making The Connection
First up, that all−important question: why? Why on earth would you want to connect your Zope system to an
external database, especially since Zope already comes with its own, very cool ZODB?
There are a couple of reasons why you might want to do this:
First, if your data is already in an existing RDBMS, you're usually going to find it tedious and
time−consuming to migrate it all to to Zope's own database. It's far easier (not to mention less disruptive to
your users) to leave your data where it is, and simply get Zope to talk to your existing RDBMS so that you
can access the data within it in a simple, transparent manner.
Second, Zope's own database is not really meant to perform the same tasks as a full−fledged RDBMS. The
ZODB is a very powerful object database which keeps track of all the different objects you use in Zope, and it
comes with some very neat transaction and versioning features. However, it's optimized to read data, not write
it, and so INSERT and UPDATE queries tend to be sub−optimal on this database.
For a more detailed discussion of why you might prefer an SQL database over the ZODB, take a look at
It should be noted at this stage that, in addition to the ZODB, Zope does come with a small, SQL−compliant
RDBMS named Gadfly, which can be used if your requirements aren't too complex. If, however, you're
dealing with large record sets or complex queries, you will probably find Gadfly too slow and inefficient for
your needs, and you will need to explore the possibility of connecting Zope to a more professional and
full−featured RDBMS.
Making The Connection
2
99234170.001.png
Adapting To The Environment
Normally, setting up a connection between Zope and an external RDBMS like MySQL, Oracle, PostgreSQL
or even Gadfly requires two things: a Database Connection object, which is used to perform communication
between Zope and the specified database, and one or more Z SQL methods, which are used to execute queries
on the database.
Zope comes with adapters for a variety of different databases, including ODBC, Oracle, MySQL and
Interbase. I'll be connecting Zope to a MySQL database in this article; however, the process is similar for
other database systems.
Hooking Zope up to MySQL is a little tedious, especially if you attempt to do it solely on the basis of the
information available online. This is because much of the online documentation relating to Zope−MySQL
connectivity is outdated and no longer relevant, and you can lose a whole day just trying out different variants
of the techniques suggested. So let me make it as simple as possible.
Getting Zope and MySQL to talk nice to each other requires you to install the following two pieces of
software:
1. The Zope MySQL Database Adapter (ZMySQLDA), available from
http://www.zope.org/Members/adustman/Products/ZMySQLDA
2. The Python MySQL Database Interface (MySQLdb), a Python MySQL driver available from
You can also find this software at http://sourceforge.net/projects/mysql−python
The installation process is fairly simple.
1. Install (copy) the ZMySQLDA files into Zope's Products directory (usually <Zope>/lib/python/Products).
2. Extract the MySQLdb files to the same place. Configure and compile the driver (instructions are available
in the source archive) using the Python binary that shipped with your version of Zope. This is extremely
important, because if you don't use the exact binary that ships with your Zope installation, Zope won't know
how to handle the driver.
3. Install the compiled driver into your Zope build (again, instructions to accomplish this are available in the
driver's documentation).
4. Restart Zope.
This process works on Linux; however, despite my best efforts, I was unable to get Zope and MySQL talking
on Windows. It's certainly possible, though, and the process is about the same − take a look at
http://zope.org/Members/philh/ZMySQLDA_html for some hints.
Assuming all goes well, you should now see a new Z MySQL Database Connection object type in the Type
drop−down list within the Zope management interface.
Adapting To The Environme...
3
99234170.002.png 99234170.003.png
Zgłoś jeśli naruszono regulamin