- Python Data Persistence - Discussion
- Python Data Persistence - Useful Resources
- Python Data Persistence - Quick Guide
- Data Persistence - Openpyxl Module
- Data Persistence - ZODB
- Python Data Persistence - Cassandra Driver
- Python Data Persistence - PyMongo module
- Python Data Persistence - SQLAlchemy
- Python Data Persistence - Sqlite3 Module
- Python Data Persistence - Plistlib Module
- Python Data Persistence - XML Parsers
- Python Data Persistence - JSON Module
- Python Data Persistence - CSV Module
- Python Data Persistence - dbm Package
- Python Data Persistence - Shelve Module
- Python Data Persistence - Marshal Module
- Python Data Persistence - Pickle Module
- Python Data Persistence - Object Serialization
- File Handling with os Module
- Python Data Persistence - File API
- Python Data Persistence - Introduction
- Python Data Persistence - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Data Persistence - dbm Package
The dbm package presents a dictionary pke interface DBM style databases. DBM stands for DataBase Manager. This is used by UNIX (and UNIX pke) operating system. The dbbm pbrary 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 pbrary 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 pke 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 431101Advertisements