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 |
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 >>> 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 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:
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 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 ( <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
Using ZODB from Zope
Posted by: daragh at 2004-07-10How does one use ZODB from within Zope - do you stil have to create a database?
Comment
db.close()
Posted by: henrique at 2005-01-31following 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.