In this article we will learn about dbm, a package in built-in library of python. dbm package in python provides a simple dictionary like interface of the form DBM (DataBase Manager) generally used in Unix operating system. dbm stores data in simple key – value pair form like a dictionary which makes it easier to insert, edit and retrieve data from database. It stores data by the use of a single primary key (“key”) in fixed size blocks.
There are three types of sub modules in dbm package :
- dbm.gnu: GNU’s reinterpretation of dbm
- dbm.ndbm: Interface based on ndbm
- dbm.dumb: Portable DBM implementation
Following are the main functions available in dbm package :
This function is used to open a dbm database or create a new database if not exist.
Syntax: dbm.open(file, flag=’r’, mode=0o666)
Parameters: This function take following parameters:
- file: name of the file.
- flag: mode of permissions. which can be ‘r’, ‘w’, ‘c’, or ‘n’.
- ‘r’: open the existing database with permission to read only.
- ‘w’: open the existing database with permission to read and write.
- ‘c’: open the database for read and write, also create a new one if it doesn’t exists.
- ‘n’: Always create a new database with permission to both read and write.
- mode: The Unix mode of the argument which is a octal form default set to 0o666, used only when new database is to be created.
Return: The corresponding object address of the database file.
This function attempts to guess which of the several simple database modules available- dbm.gnu, dbm.ndbm, or dbm.dumb- should be used to open a given file.
Parameter: filename- Name of the file.
Returns: The function returns one of the following values :
- None: If the database doesn’t exists or it can’t be opened.
- (‘ ‘): An empty string, if the file exists but the file format can’t be guessed else
- The required module name: If the type is successfully detected then one of the string names is returned, ‘dbm.gnu’, ‘dbm.ndbm’ or ‘dbm.dumb’.
Following are the built-in methods for dbm objects :
- items(): This method returns the items contained in the database of caller object in form of key value pairs. (db is the caller object database).
- clear(): clear all the values present in the database.
- get(key): returns the value corresponding to key given in argument.
- keys(), iterkeys(): returns an iterable list containing keys of the dictionary.
- pop(key): Deletes / pops the key, value pair corresponding to key given in argument.
- setdefault(): set a default primary key given in the argument.
- sync(): Helps to synchronize data files and on disk directory.
- update(): updates the existing key value. Just like dictionary object.
- values(): iterate through all the values present in database.
- close(): Doesn’t take any argument nor returns anything. Just closes the caller object database. (db in this case)
Below is the implementation of all the above discuss methods/functions:
b'GeeksforGeeks' b'GeeksforGeeks' b'8888' b'GfG' b'01/01/2000' b'GeeksforGeeks' b'8888' b'GfG' b'01/01/2000' b'name' b'GeeksforGeeks' b'Short name' b'GfG' b'Date' b'01/01/2000'
- Simple Multithreaded Download Manager in Python
- Context Manager in Python
- Python VLC MediaPlayer - Getting Event Manager object
- Context Manager Using @contextmanager Decorator
- NetworkX : Python software package for study of complex networks
- Python | Plotting Google Map using gmplot package
- Create and Access a Python Package
- Python | Plotting Google Map using folium package
- Python | Plotting Data on Google Map using pygmaps package
- Python | Adding markers to volcano locations using folium package
- Install Python package using Jupyter Notebook
- How to install MySQL connector package in Python?
- Humanize Package in Python
- What is the difference between Python's Module, Package and Library?
- Plotting ICMR approved test centers on Google Maps using folium package
- How to Install SQLObject package on Windows and Linux?
- Unicodedata – Unicode Database in Python
- Python MySQL - Create Database
- Oracle Database Connection in Python
- Inserting variables to database table using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.