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

Log in
Name

Password

 

How To Love ZODB and Forget RDBMS - Part IV

 

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

Show Me the Money!
  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

Now that we've stored data in the database, how do we get ZODB to show it to us? Let's recap first; here's the python code we have entered in interactive mode so far:

 >>> import ZODB.config
 >>> db = ZODB.config.databaseFromURL('pymonitor.conf')
 >>> conn = db.open()
 >>> dbroot = conn.root()
 >>> from BTrees.OOBTree import OOBTree
 >>> dbname = 'monitoring_db'
 >>> dbroot[dbname] = OOBTree()
 >>> mondb = dbroot[dbname]
 >>> from Persistence import Persistent
 >>> class ServiceTable(Persistent):
 ...  def __init__(self):
 ...    self.id = ''
 ...    self.name = ''
 ...    self.description = ''
 ...    self.service = ''
 ...    self.hostname = ''
 ...    self.state_current = ''
 ...    self.state_last = ''
 ...    self.time_lastcheck = ''
 ...    self.time_lastok = ''
 ...    self.time_lastwarn = ''
 ...    self.time_lasterror = ''
 ... 
 >>>
 >>>
 >>> newmon = ServiceTable()
 >>> newmon.id = 'myserver.hostingcompany.com-httpd' 
 >>> newmon.name = 'Apache Web Server'
 >>> newmon.description = 'This is the staging Web Server Service for 16 low-volume clients'
 >>> newmon.service = 'httpd'
 >>> newmon.hostname = 'myserver'
 >>> newmon.state_current = 'OK'
 >>> newmon.state_last = 'OK'
 >>> mondb[newmon.id] = newmon
 >>> get_transaction().commit()

It's starting to add up! What we want to do next is prove to ourselves that this really stored everything and that we can get it back, even if we quit the python interactive mode. So let's do it - hit ^D to exit python and go back to your command prompt.

Now that you've done that, let's start up python again and re-type some of the commands:

 >>> import ZODB.config
 >>> db = ZODB.config.databaseFromURL('pymonitor.conf')
 >>> conn = db.open()
 >>> dbroot = conn.root()
 >>> dbname = 'monitoring_db'
 >>> mondb = dbroot[dbname]
 >>> from Persistence import Persistent
 >>> class ServiceTable(Persistent):
 ...  def __init__(self):
 ...    self.id = ''
 ...    self.name = ''
 ...    self.description = ''
 ...    self.service = ''
 ...    self.hostname = ''
 ...    self.state_current = ''
 ...    self.state_last = ''
 ...    self.time_lastcheck = ''
 ...    self.time_lastok = ''
 ...    self.time_lastwarn = ''
 ...    self.time_lasterror = ''
 ... 
 >>> 
 >>> 

Now we have started a new python session, loaded our database "server" (the ZODB file), have connected to it, and have handle to the monitoring_db database that we created. However, you will notice that we don't import the BTree module and we don't create the monitoring_db database, since it's already been created. Additionally, we have redefined the class that we used to store our data. The reason for this is that the 'ServiceTable schema is saved in the database and if we try to retrieve it without defining it, Python won't know what to do with it.

Now, let's query our database:

 >>> myoldmon = mondb['myserver.hostingcompany.com-httpd']
 >>> myoldmon.name
 'Apache Web Server'
 >>> myoldmon.description
 'This is the staging Web Server Service for 16 low-volume clients'
 >>> myoldmon.id
 'myserver.hostingcompany.com-httpd'
 >>> myoldmon.hostname
 'myserver'
 >>> myoldmon.service    
 'httpd'

Code Breakdown : We get our table back by giving the database the table's unique id myserver.hostingcompany.com-httpd . We assign this ServiceTable data to the variable name myoldmon, and once that's done we simply print out some fields to prove to ourselves that we actually stored the data. Vio-la!

On to Part V