# 7.12 `bsddb` -- Interface to Berkeley DB library

Availability: Unix, Windows.

The `bsddb` module provides an interface to the Berkeley DB library. Users can create hash, btree or record based library files using the appropriate open call. Bsddb objects behave generally like dictionaries. Keys and values must be strings, however, so to use other objects as keys or to store other kinds of objects the user must serialize them somehow, typically using marshal.dumps or pickle.dumps.

There are two incompatible versions of the underlying library. Version 1.85 is widely available, but has some known bugs. Version 2 is not quite as widely used, but does offer some improvements. The `bsddb` module uses the 1.85 interface. Starting with Python 2.0, the configure script can usually determine the version of the library which is available and build it correctly. If you have difficulty getting configure to do the right thing, run it with the --help option to get information about additional options that can help. On Windows, you will need to define the `HAVE_DB_185_H` macro if you are building Python from source and using version 2 of the DB library.

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.

`hashopen`(filename[, flag[, mode[, bsize[, ffactor[, nelem[, cachesize[, hash[, lorder]]]]]]]])
  Open the hash format file named 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) 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.

`btopen`(filename[, flag[, mode[, btflags[, cachesize[, maxkeypage[, minkeypage[, psize[, lorder]]]]]]]])

  Open the btree format file named 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) 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.

`rnopen`(filename[, flag[, mode[, rnflags[, cachesize[, psize[, lorder[, reclen[, bval[, bfname]]]]]]]]])

  Open a DB record format file named 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) 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.

See Also:

Module `dbhash`:
  DBM-style interface to the `bsddb`.

Subsections

- 7.12.1 Hash, BTree and Record Objects