Berkeley DB is an open source database. Its most important advantages are its simplicity to use and its performance.
This is an introductory example, that shows how to create a database, add new elements in it (as Key/Value pairs) and finally how to print all content of the database. The example is divided in two independent part.
Python, 41 lines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#!/usr/bin/python from bsddb3 import db # the Berkeley db data base # Part 1: Create database and insert 4 elements # filename = 'fruit' # Get an instance of BerkeleyDB fruitDB = db.DB() # Create a database in file "fruit" with a Hash access method # There are also, B+tree and Recno access methods fruitDB.open(filename, None, db.DB_HASH, db.DB_CREATE) # Print version information print '\t', db.DB_VERSION_STRING # Insert new elements in database fruitDB.put("apple","red") fruitDB.put("orange","orange") fruitDB.put("banana","yellow") fruitDB.put("tomato","red") # Close database fruitDB.close() # Part 2: Open database and write its contents out # fruitDB = db.DB() # Open database # Access method: Hash # set isolation level to "dirty read (read uncommited)" fruitDB.open(filename, None, db.DB_HASH, db.DB_DIRTY_READ) # get database cursor and print out database content cursor = fruitDB.cursor() rec = cursor.first() while rec: print rec rec = cursor.next() fruitDB.close()
anydbm. Couldn't you do all this more portably with anydbm? I think BSDDB has some special stuff, but I can't see it showing in your recipe.
anydbm. Unfortunately, the anydbm works only for tiny tables (less than 1000 records). It experiences serious data loss after a session to DB has been closed with records > 1,500. I definitely need to try the one with Berkley's solution - thank you
There is also just the regular 'bsddb', with 'btopen', 'hashopen', and 'rnopen'...as has been listed and documented in the standard library for at least 5 years. What happened to reading the global module listing to discover what is available in standard Python?
regular bsddb with btopen etc. are now 'legacy' code. From Python 2.4 module documentation...
"http://pybsddb.sourceforge.net/ Website with documentation for the new python Berkeley DB interface that closely mirrors the sleepycat object oriented interface provided in Berkeley DB 3 and 4.
The following is a description of the legacy bsddb interface compatible with the old python bsddb module. For details about the more modern Db and DbEnv object oriented interface see the above mentioned pybsddb URL."
what this example really needs. is how to lock, unlock records and detect deadlock.
the interface is a little obtuse and comments in dbutils leaves one with little confidence.
The provided Python example is ancient, but was still valuable in getting into BdB for the first time. I would suggest adding another line after line 13:
This makes sure that fruitDB starts out empty. It might not be if the file designated by filename already exists, which can lead to some extended head scratching.