You are not logged in Log in Join
You are here: Home » Members » adytumsolutions » How To Love ZODB and Forget RDBMS - Part II

Log in
Name

Password

 

How To Love ZODB and Forget RDBMS - Part II

 

Created by adytumsolutions . Last modified 2004-03-01 17:50:20.

Addressing the But-Where-Do-I-Begin Syndrome
  1. Introduction
  2. Creating/Opening a Stand-alone ZODB
  3. Adding Data to a ZODB
  4. Retrieving Data from a ZODB
  5. Using the ZODB Tools to inspect the ZODB
  6. Using the ZODB Tools to Backup and Recover the ZODB
  7. Using Stand-alone ZEO to Allow Many Connections
  8. Acknowledgements

Python

For those of you not familiar with python, the time has arrived! There are some great tutorials out there that you can knock out in an afternoon:

For people with a programming background, you can learn python in about two days. For those of you new to programming, your mileage will vary - however, you will learn python faster than any other language.

Installing the ZODB Libraries

Though we think that everyone should learn python ('cause it's just so fun!), there are very practical reasons for learning python when working with ZODB databases: you can't access it without it! We will be discussing ZODB 3.2 and this requires Python 2.3.3 (which we recommend you install; it's the latest production release).

If you have other applications that need to run with different versions of Python (like Zope 2.6 or earlier), you need not worry; you can have multiple installations of python on your machine. During the installtion/configuration process you will need to indicate a new path for python to install.

Once python is installed, you will need to download and install ZODB (see the ZODB Wiki). See the docs for the latest intructions, but the usual thing to do is change directory to the one you un'tar'ed from your download and then run the following:

 python setup.py build
 python setup.py install

Before we continue (once you have successfully installed ZODB) you need to exit the directory in which you just did the install. Otherwise, you will get weird conflicts and errors once you go into Python's interactive mode and import ZODB and related modules.

Creating a ZODB

The following was taken from Andrew Kuchling's ZODB & ZEO introduction - we highly recommend you read this document as well as the other resources available to you on on the ZODB Wiki .

Creating a ZODB database is as easy as deciding where to save the file. In python interactive mode (just type python at the command prompt), enter the following:

 >>> from ZODB import FileStorage, DB
 >>> storage = FileStorage.FileStorage('/tmp/pymonitor.fs') 
 >>> db = DB(storage)

Code Breakdown : We need to load some python code that the Good ZODB Developer Folks wrote. The code is in the form of modules, or python library files. The two we are interested in here are FileStorage and DB. Once these are loaded, we can begin. The second line creates a file called /tmp/pymonitor.fs (and other supporting files; after you enter this line of code, look in the /tmp directory and you'll see them) and sets it up so that it can be used to store data. If you have already done this (i.e., if the file exists), it doesn't create it, it just opens it. In the third line, we establish a relationship between the physical file handle (and other stuff) stored in storage and the Object Database. This third line is where the database is a useful entity is actually created.

You just created a ZODB database!

Opening a ZODB

Great, but how do we open it and use it? Very easily! Here are the steps we have to take next:

  • Create/Open a file for the database
  • Create a connection to the database
  • Set the root of the database (think of this as a "mount point"):
     >>> conn = db.open()
     >>> dbroot = conn.root()
    

Code Breakdown : The database has already been created, but in order to use it, we need something to manage all connections to the database. The first line of code here does just that: we open a connection to the database and assign that connection the name conn. Now that the database has been created, opened, and connected to, we are going to need to know where to put things. Remember that object oriented databases are hierarchies, and setting the root is like declaring the top of the hierarchy to which all else in the database will ultimately be connected.

So, we have created and opened a database, and all with just a few lines of code.

Most of the times, though, we don't want to have to edit code to configure a database; it'd be much nicer to have a config file for this. So, let's do the same thing we did above, but this time use a config file.

First, you need to exit out of interactive python mode (^D) and create a new file in your current directory. Let's call it pymonitor.conf (the reason for that will become appearant later...). Give it the following contents:

 <zodb>
   <filestorage>
     path /tmp/pymonitor.fs
   </filestorage>
 </zodb>

Save this file and then go back into Python interpreter mode. Now we can accomplish the same thing as above, but with a configuration file instead. Due to the fact that we are using a config file, our code will be a little different:

 >>> import ZODB.config
 >>> db = ZODB.config.databaseFromURL('pymonitor.conf')

Code Breakdown : First we load the module we need (only one, this time). In the second line, we tell python to find all the information it needs in order to create or open a database file on the file system and set it up for storing data.

The rest is the same:

 >>> conn = db.open()
 >>> dbroot = conn.root()

We now know how to open (and implicitly create) ZODBs. Next we want to add data to our new database...

On to Part III

Comment

Discussion icon Using ZODB from Zope

Posted by: daragh at 2004-07-10

How does one use ZODB from within Zope - do you stil have to create a database?

Comment

Discussion icon db.close()

Posted by: henrique at 2005-01-31

following this tutorial/guide, i got stuck in this stage "db = ZODB.config.databaseFromURL(pymonitor.conf)" because i got an error "No handlers could be found for logger "ZODB.FileStorage"", after i quickly found in google that i forgot to type: db.close() in the last tutorial task.

maybe it's a good idea to add it in this tutorial. db.close() before ^D in python interpreter mode. so, the newbies won't get stuck on it.