Recipe 7.8. Using the Berkeley DB Database
Credit: Farhad Fouladi
You want to persist some data, exploiting the simplicity and good performance of the Berkeley DB database library.
If you have previously installed Berkeley DB on your machine, the Python Standard Library comes with package
try: from bsddb import db # first try release 4 except ImportError: from bsddb3 import db # not there, try release 3 instead print db.DB_VERSION_STRING # emits, e.g: Sleepycat Software: Berkeley DB 4.1.25: (December 19, 2002)
To create a database, instantiate a
object, then call its method
adb = db.DB( ) adb.open('db_filename', dbtype=db.DB_HASH, flags=db.DB_CREATE)
is just one of several access
Once you have an open instance of
, you can add records, each
for i, w in enumerate('some words for example'.split( )): adb.put(w, str(i))
You can access records via a cursor on the database:
def irecords(curs): record = curs.first( ) while record: yield record record = curs.next( ) for key, data in irecords(adb.cursor( )): print 'key=%r, data=%r' % (key, data) # emits (the order may vary): # key='some', data='0' # key='example', data='3' # key='words', data='1' # key='for', data='2'
When you're done, you close the database:
At any future time, in the same or another Python program, you can reopen the database by giving just its filename as the argument to the open method of a newly created db.DB instance:
the_same_db = db.DB( ) the_same_db.open('db_filename')
and work on it again in the same ways:
the_same_db.put('skidoo', '23') # add a record the_same_db.put('words', 'sweet') # replace a record for key, data in irecords(the_same_db.cursor( )): print 'key=%r, data=%r' % (key, data) # emits (the order may vary): # key='some', data='0' # key='example', data='3' # key='words', data='sweet' # key='for', data='2' # key='skidoo', data='23'
Again, remember to close the database when you're done:
The Berkeley DB is a popular open source database. It does not support SQL, but it's simple to use, offers
The Python Standard Library
module can use the Berkeley DB as its underlying database engine, just as it uses
for serialization. However,
does not let you take advantage of the ability to access a Berkeley DB database file from several different languages, exactly because the records are strings produced by
For example, creating a database with an access method of
, as shown in the recipe, may give maximum performance, but, as you'll have noticed when listing all records with the generator
that is also presented in the recipe, hashing puts records in apparently random, unpredictable order. If you need to access records in sorted order, you can use an access method of
instead. Berkeley DB also supports more advanced functionality, such as transactions, which you can enable through direct access but not via
For detailed documentation about all functionality of the Python Standard Library
package, see http://pybsddb.
Library Reference and Python in a Nutshell docs for modules anydbm , shelve , and bsddb ; http://pybsddb.sourceforge.net/bsddb3.html for many more details about bsddb and bsddb3 ; http://www.sleepycat.com/ for downloads of, and very detailed documentation on, the Berkeley DB itself.
Recipe7.8.Using the Berkeley DB Database