Python Data Persistence 简明教程
Python Data Persistence - dbm Package
dbm 包提供类似词典的界面,即 DBM 样式数据库。 DBM stands for DataBase Manager 。UNIX(以及类似 UNIX 的)操作系统使用这种形式。dbbm 库是由 Ken Thompson 编写的一个简单的数据库引擎。这些数据库使用二进制编码的字符串对象作为键和值。
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.
dbm 包包含以下模块 −
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()
open() 函数允许使用这些标志进行模式化 −
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 |
dbm 对象是类似词典的对象,就像内容对象。因此,所有词典操作均可执行。dbm 对象可以调用 get()、pop()、append() 和 update() 方法。以下代码使用“r”标志打开“mydbm.db”,然后迭代键值对集合。
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'