Python Interfaces to Unix databases (dbm)

PythonServer Side ProgrammingProgramming

The dbm package in Python's built-in library provides a dictionary like an interface DBM style databases. The dbm library is a simple database engine, written by Ken Thompson. DBM stands for DataBase Manager, used by UNIX operating system, the library stores arbitrary data by use of a single key (a primary key) in fixed-size buckets and uses hashing techniques to enable fast retrieval of the data by key.

There are following modules in dbm package −

The dbm.ndbm module provides an interface to the Unix “(n)dbm” library. Dbm objects behave like dictionaries, with keys and values should be stored as bytes. The module doesn't support and the items() and values() methods.

The dbm.dumb module provides a persistent dictionary-like interface which is written entirely in Python. Unlike other modules such as dbm.gnu no external library is required. As with other persistent mappings, the keys and values are always stored as bytes.

These modules are internally used by Python's shelve module. As in the case of shelve database, user-specified database name carries '.dir' postfix. The dbm object's whichdb() function tells which implementation of dbm is available on current Python installation.

>>> dbm.whichdb('mydbm.db')

'dbm.dumb'

>>> db = dbm.open('mydbm.db','n')
>>> db['name'] = Rajani Deshmukh'
>>> db['address'] = 'Shivajinagar Pune'
>>> db['PIN'] = '431001'
>>> db.close()

The open() function allows mode these flags −

Value
Meaning
'r'
Open an existing database for reading only (default)
'w'
Open an existing database for reading and writing
'c'
Open database for reading and writing, creating it if it doesn’t exist
'n'
Always create a new, empty database, open for reading and writing

A dbm object is a dictionary like an object, just as a shelf object. Hence all dictionary operations can be performed. The dbm object can invoke get(),pop(), append(0 and update() methods. Following code opens 'mydbm.db' with 'r' flag and iterates over the collection of key-value pairs.

>>> db = dbm.open('mydbm.db','r')
>>> for k,v in db.items():
print (k,v)
b'name' : Rajani Deshmukh'
b'address' : b'Shivajinagar Pune'
b'PIN' : b'431001'

dbm objects also provide the following methods −

sync(): Synchronize the on-disk directory and data files. This method is called by the Shelve.sync() method.

close(): Close the dbm database.

gnu dbm objects have the following methods −

firstkey()

It’s possible to loop over every key in the database using this method and the nextkey() method. This method returns the starting key.

gdbm.nextkey(key): Returns the key that follows key in the traversal.

gdbm.reorganize(): this function will reorganize the database. gnu dbm objects will not shorten the length of a database file except by using this reorganization; otherwise, deleted file space will be kept and reused as new (key, value) pairs are added.

raja
Updated on 30-Jul-2019 22:30:25

Advertisements