Python Dbm Module

Last Updated : 25 Sept 2026

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.

What is the DBM Module?

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.

Types of Sub-modules from DBM Module

The DBM module includes four types of sub-modules. Let's discuss each of them below:

1. Dbm.sqlite3: SQLite Backend

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.

2. dbm.gnu: GNU’s reinterpretation of dbm

The dbm.gnu module provides an interface to the GNU DBM (GDBM) library. It allows Python programs to create and access DBM-style databases.

3. dbm.ndbm: Interface based on ndbm

The dbm.ndbm module provides an interface to the Unix ndbm database implementation. It allows Python programs to work with DBM-style databases.

4. dbm.dumb: Portable DBM implementation

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.

Functions of the DBM Module

The following are the main functions of the dbm module. Let’s see them in detail.

1. dbm.open()

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:

  • file: It's the name of the file.
  • flag: The flag parameter specifies the mode used to open the DBM database. The available modes are:
PermissionsDescriptions
'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.
  • mode: The mode parameter specifies the Unix file permission mode in octal format. Its default value is 0o666, and it is used only when creating a new database.

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()}")
Execute Now

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.

2. dbm.whichdb()

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:

  • filename: It’s the name of the file.

Return Value

It returns one of the following values:

  • None: The None value is obtained when the database doesn't exist, or it can't be opened.
  • (''): It is an empty string, which is obtained when the file exists, but the file format can't be guessed.
  • 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)
Execute Now

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.

Built-in methods for the DBM Object

The following table shows important built-in methods of the DBM object for accessing data.

MethodsDescription
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.