How To Love ZODB and Forget RDBMS - Part IV |
Created by adytumsolutions . Last modified 2004-03-01 17:50:20. |
Show Me the Money! |
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 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 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 On to Part V |