
- Python Data Persistence Tutorial
- Python Data Persistence - Home
- Python Data Persistence - Introduction
- Python Data Persistence - File API
- File Handling with os Module
- Python Data Persistence - Object Serialization
- Python Data Persistence - Pickle Module
- Python Data Persistence - Marshal Module
- Python Data Persistence - Shelve Module
- Python Data Persistence - dbm Package
- Python Data Persistence - CSV Module
- Python Data Persistence - JSON Module
- Python Data Persistence - XML Parsers
- Python Data Persistence - Plistlib Module
- Python Data Persistence - Sqlite3 Module
- Python Data Persistence - SQLAlchemy
- Python Data Persistence - PyMongo module
- Python Data Persistence - Cassandra Driver
- Data Persistence - ZODB
- Data Persistence - Openpyxl Module
- Python Data Persistence Resources
- Python Data Persistence - Quick Guide
- Python Data Persistence - Useful Resources
- Python Data Persistence - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Data Persistence - dbm Package
The dbm package presents a dictionary like interface DBM style databases. DBM stands for DataBase Manager. This is used by UNIX (and UNIX like) operating system. The dbbm library is a simple database engine written by Ken Thompson. These databases use binary encoded string objects as key, as well as value.
The database stores 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.
The dbm package contains following modules −
dbm.gnu module is an interface to the DBM library version as implemented by the GNU project.
dbm.ndbm module provides an interface to UNIX nbdm implementation.
dbm.dumb is used as a fallback option in the event, other dbm implementations are not found. This requires no external dependencies but is slower than others.
>>> dbm.whichdb('mydbm.db') 'dbm.dumb' >>> import dbm >>> db=dbm.open('mydbm.db','n') >>> db['name']=Raj Deshmane' >>> db['address']='Kirtinagar Pune' >>> db['PIN']='431101' >>> db.close()
The open() function allows mode these flags −
Sr.No. | Value & Meaning |
---|---|
1 |
'r' Open existing database for reading only (default) |
2 | 'w' Open existing database for reading and writing |
3 | 'c' Open database for reading and writing, creating it if it doesn’t exist |
4 | 'n' Always create a new, empty database, open for reading and writing |
The dbm object is a dictionary like object, just as shelf object. Hence, all dictionary operations can be performed. The dbm object can invoke get(), pop(), append() and update() methods. Following code opens 'mydbm.db' with 'r' flag and iterates over collection of key-value pairs.
>>> db=dbm.open('mydbm.db','r') >>> for k,v in db.items(): print (k,v) b'name' : b'Raj Deshmane' b'address' : b'Kirtinagar Pune' b'PIN' : b'431101'