模块 bsddb 提供了 Berkeley 数据库的接口。用户可以使用相应的 open 调用创建基于 hash、 btree 或 record 的数据库文件。 Bsddb 对象的主要行为方式与字典相似。然而键与值都必须是字符串,因此要将其他对象用作键或储存其他类型的对象,用户必须将它们序列化,通常使用 marshal.dumps() 或者 pickle.dumps() 。
模块 bsddb 对 Berkeley 数据库版本要求介于 3.3 至 4.5 之间。
See also
该站点载有 bsddb.db Python Berkeley DB 接口的文档,该接口几乎映射了 Berkeley DB 3 和 4 面向对象接口(的全部功能)。
Berkeley DB 类库。
A more modern DB, DBEnv and DBSequence object interface is available in the bsddb.db module which closely matches the Berkeley DB C API documented at the above URLs. Additional features provided by the bsddb.db API include fine tuning, transactions, logging, and multiprocess concurrent database access.
The following is a description of the legacy bsddb interface compatible with the old Python bsddb module. Starting in Python 2.5 this interface should be safe for multithreaded access. The bsddb.db API is recommended for threading users as it provides better control.
The bsddb module defines the following functions that create objects that access the appropriate type of Berkeley DB file. The first two arguments of each function are the same. For ease of portability, only the first two arguments should be used in most instances.
Open the hash format file named filename. Files never intended to be preserved on disk may be created by passing None as the filename. The optional flag identifies the mode used to open the file. It may be 'r' (read only), 'w' (read-write) , 'c' (read-write - create if necessary; the default) or 'n' (read-write - truncate to zero length). The other arguments are rarely used and are just passed to the low-level dbopen() function. Consult the Berkeley DB documentation for their use and interpretation.
Open the btree format file named filename. Files never intended to be preserved on disk may be created by passing None as the filename. The optional flag identifies the mode used to open the file. It may be 'r' (read only), 'w' (read-write), 'c' (read-write - create if necessary; the default) or 'n' (read-write - truncate to zero length). The other arguments are rarely used and are just passed to the low-level dbopen function. Consult the Berkeley DB documentation for their use and interpretation.
Open a DB record format file named filename. Files never intended to be preserved on disk may be created by passing None as the filename. The optional flag identifies the mode used to open the file. It may be 'r' (read only), 'w' (read-write), 'c' (read-write - create if necessary; the default) or 'n' (read-write - truncate to zero length). The other arguments are rarely used and are just passed to the low-level dbopen function. Consult the Berkeley DB documentation for their use and interpretation.
Wrapper class around a DB object that supports string keys (rather than bytes). All keys are encoded as UTF-8, then passed to the underlying object.
Wrapper class around a DB object that supports string values (rather than bytes). All values are encoded as UTF-8, then passed to the underlying object.
See also
DBM-style interface to the bsddb
Once instantiated, hash, btree and record objects support the same methods as dictionaries. In addition, they support the methods listed below.
Return True if the DB file contains the argument as a key.
Close the underlying file. The object can no longer be accessed. Since there is no open open() method for these objects, to open the file again a new bsddb module open function must be called.
Return the list of keys contained in the DB file. The order of the list is unspecified and should not be relied on. In particular, the order of the list returned is different for different file formats.
Set the cursor to the item indicated by key and return a tuple containing the key and its value. For binary tree databases (opened using btopen()), if key does not actually exist in the database, the cursor will point to the next item in sorted order and return that key and value. For other databases, KeyError will be raised if key is not found in the database.
Set the cursor to the first item in the DB file and return it. The order of keys in the file is unspecified, except in the case of B-Tree databases. This method raises bsddb.error if the database is empty.
Set the cursor to the next item in the DB file and return it. The order of keys in the file is unspecified, except in the case of B-Tree databases.
Set the cursor to the previous item in the DB file and return it. The order of keys in the file is unspecified, except in the case of B-Tree databases. This is not supported on hashtable databases (those opened with hashopen()).
Description: DB_SEQUENCE->open The DB_SEQUENCE->open method opens the sequence represented by the key. The key must be compatible with the underlying database specified in the corresponding call to db_sequence_create.
Parameters key The key specifies which record in the database stores the persistent sequence data. flags The flags parameter must be set to 0 or by bitwise inclusively OR’ing together one or more of the following values: DB_AUTO_COMMIT Enclose the DB_SEQUENCE->open call within a transaction. If the call succeeds, the open operation will be recoverable. If the DB_CREATE flag is specified and the call fails, no sequence will have been created. DB_CREATE Create the sequence. If the sequence does not already exist and the DB_CREATE flag is not specified, the DB_SEQUENCE->open will fail. DB_EXCL Return an error if the sequence already exists. The DB_EXCL flag is only meaningful when specified with the DB_CREATE flag. DB_THREAD Cause the DB_SEQUENCE handle returned by DB_SEQUENCE->open to be free-threaded; that is, usable by multiple threads within a single address space. txnid If the operation is to be transaction-protected, (other than by specifying the DB_AUTO_COMMIT flag), the txnid parameter is a transaction handle returned from DB_ENV->txn_begin; otherwise, NULL. Note that transactionally protected operations on a DB_SEQUENCE handle require the DB_SEQUENCE handle itself be transactionally protected during its open if the open creates the sequence.
Synchronize the database on disk.
Example:
>>> import bsddb
>>> db = bsddb.btopen('/tmp/spam.db', 'c')
>>> for i in range(10):
... db[str(i)] = '%d' % (i*i)
...
>>> db['3']
'9'
>>> db.keys()
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> db.first()
('0', '0')
>>> db.next()
('1', '1')
>>> db.last()
('9', '81')
>>> db.set_location('2')
('2', '4')
>>> db.previous()
('1', '1')
>>> for k, v in db.iteritems():
... print(k, v)
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
>>> '8' in db
True
>>> db.sync()
0
关于本评注系统
本站使用上下文关联的评注系统来收集反馈信息。不同于一般对整章做评注的做法, 我们允许你对每一个独立的“文本块”做评注。一个“文本块”看起来是这样的:
一个“文本块”是一个段落,一个列表项,一段代码,或者其他一小段内容。 你选中它会高亮度显示:
要对文本块做评注,你只需要点击它旁边的标识块:
我们会仔细阅读每个评论,如果可能的话我们也会把评注考虑到未来的版本中去:
如果你愿意你的评注被采用,请确保留下你的全名 (注意不是昵称或简称)
Many, many thanks to Jack Slocum; the inspiration and much of the code for the comment system comes from Jack's blog, and this site couldn't have been built without his wonderful
YAHOO.extlibrary. Thanks also to Yahoo for YUI itself.