In Python, the dbm module provides a dictionary-like interface for working with DBM-style databases. It allows us to store data as key-value pairs and save them persistently in disk files.
In this chapter, we will discuss the dbm module, its sub-modules, main functions, examples, and built-in methods.
The dbm module in Python creates and manages persistent key-value databases. It works like a dictionary, but it stores data in a file and allows us to access it even after the program ends.
It is commonly used on Unix and Unix-like operating systems. It provides a simple database engine for storing data as key-value pairs, where both keys and values are stored as binary-encoded strings.
The database stores data using a single key (primary key) in a fixed-size container. It uses hashing techniques to retrieve data quickly using the key.
The DBM module includes four types of sub-modules. Let's discuss each of them below:
The dbm.sqlite3 module provides a DBM interface using SQLite as the backend. It was added in Python 3.13. The database files created by this module can also be opened using the standard sqlite3 module and other SQLite tools.
The dbm.gnu module provides an interface to the GNU DBM (GDBM) library. It allows Python programs to create and access DBM-style databases.
The dbm.ndbm module provides an interface to the Unix ndbm database implementation. It allows Python programs to work with DBM-style databases.
The dbm.dumb module is an alternative option when other dbm implementations are not available. It does not depend on any external packages, but it may be slower than other DBM implementations.
The following are the main functions of the dbm module. Let’s see them in detail.
The dbm.open() function opens an existing DBM database or creates a new one. It returns a database object that allows us to store, access, update, and delete key-value pairs.
Syntax
It has the following syntax:
#dbm.open(file, flag='r', mode=0o666)
Parameters
The open() function takes the following parameters:
| Permissions | Descriptions |
|---|---|
| 'r' | It opens the existing database with read-only permission. |
| 'w' | It opens the existing database with read and write permissions. |
| 'c' | It opens the database for reading and writing and creates a new database if it does not already exist. |
| 'n' | It always creates a new database with read and write permissions. |
Return Value
It returns a database object.
Example
The following example shows how to open an existing DBM database to update or modify the database.
Python
# Import the dbm module
import dbm
# Open or create a DBM database file
db = dbm.open("company", "c")
# Access the database
for key, val in db.items():
print(f"{key.decode()}: {val.decode()}")
Output:
location: India name: Tpoint Tech website: https://www.tpointtech.com
Explanation
In this example, we imported the dbm module and opened the database file using the 'c' flag, which allows us to read from and write to the database. After that, we stored the database object in the db variable and displayed the output.
The dbm.whichdb() function determines which DBM implementation, such as dbm.gnu, dbm.ndbm, or dbm.dumb, should be used to open a specified database file.
Syntax
It has the following syntax:
#dbm.whichdb(filename)
Parameters
The whichdb() function takes the following parameters:
Return Value
It returns one of the following values:
The required module name: It is obtained when the type is successfully detected; then one of the string names is returned: 'dbm.gnu', 'dbm.ndbm', or 'dbm.dumb'.
Example
The following example demonstrates how to use the whichdb() function to identify an existing DBM database and determine which DBM implementation should be used to open the specified database file.
Python
# Import the required module.
import dbm
import dbm.dumb
# Create and open a DBM database
db = dbm.dumb.open("company_db", "c")
# Add company details
db["name"] = "Tpoint Tech"
db["location"] = "India"
# Close the database
db.close()
# Check which DBM database format is used
database_type = dbm.whichdb("company_db")
# Display the database type
print("Database Type:", database_type)
Output:
Database Type: dbm.dumb
Explanation
In this example, we imported the module and created a database named company_db with read and write permissions. After that, we added some company details and closed the connection. Finally, we checked which DBM database is used by using the whichdb() function and displayed it in the output.
The following table shows important built-in methods of the DBM object for accessing data.
| Methods | Description |
|---|---|
| get(key) | The get() method returns the value associated with the specified key. |
| keys() | The keys() method returns an iterable containing all the keys in the DBM database. |
| firstkey() | The firstkey() method returns the first key in the DBM database. |
| nextkey(key) | The nextkey() method returns the key that comes immediately after the specified key. |
| setdefault() | The setdefault() method sets a default value for the specified key if the key does not already exist. |
| reorganize() | The reorganize() method reorganizes the database by compacting the stored data to free up space. |
| error() | The error() method returns a tuple containing the exceptions that can be raised when an error occurs while using the dbm module. |
| sync() | The sync() method synchronizes the database data with the files stored on disk. |
| close() | The close() method does not take any arguments or return a value. It closes the DBM database object. |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India