library/bsddb

bsddb — Berkeley 数据库接口

模块 bsddb 提供了 Berkeley 数据库的接口。用户可以使用相应的 open 调用创建基于 hash、 btree 或 record 的数据库文件。 Bsddb 对象的主要行为方式与字典相似。然而键与值都必须是字符串,因此要将其他对象用作键或储存其他类型的对象,用户必须将它们序列化,通常使用 marshal.dumps() 或者 pickle.dumps()

模块 bsddb 对 Berkeley 数据库版本要求介于 3.3 至 4.5 之间。

See also

http://pybsddb.sourceforge.net/

该站点载有 bsddb.db Python Berkeley DB 接口的文档,该接口几乎映射了 Berkeley DB 3 和 4 面向对象接口(的全部功能)。

http://www.oracle.com/database/berkeley-db/

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.

bsddb.hashopen(filename[, flag[, mode[, pgsize[, ffactor[, nelem[, cachesize[, lorder[, hflags]]]]]]]])

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.

bsddb.btopen(filename[, flag[, mode[, btflags[, cachesize[, maxkeypage[, minkeypage[, pgsize[, lorder]]]]]]]])

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.

bsddb.rnopen(filename[, flag[, mode[, rnflags[, cachesize[, pgsize[, lorder[, rlen[, delim[, source[, pad]]]]]]]]]])

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.

class bsddb.StringKeys(db)

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.

class bsddb.StringValues(db)

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

Module dbhash

DBM-style interface to the bsddb

Hash, BTree and Record Objects

Once instantiated, hash, btree and record objects support the same methods as dictionaries. In addition, they support the methods listed below.

key in bsddbobject

Return True if the DB file contains the argument as a key.

bsddbobject.close()

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.

bsddbobject.keys()

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.

bsddbobject.set_location(key)

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.

bsddbobject.first()

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.

bsddbobject.next()

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.

bsddbobject.previous()

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()).

bsddbobject.last()

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.

bsddbobject.sync()

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