blob: 925e51269db549ec47d756ba5c0f95a19ef5310c [file] [log] [blame]
Fred Drake9d158811999-04-19 21:19:21 +00001\section{\module{bsddb} ---
2 Interface to Berkeley DB library}
3
4\declaremodule{extension}{bsddb}
5 \platform{Unix, Windows}
6\modulesynopsis{Interface to Berkeley DB database library}
7\sectionauthor{Skip Montanaro}{skip@mojam.com}
8
9
Fred Drake38e5d272000-04-03 20:13:55 +000010The \module{bsddb} module provides an interface to the Berkeley DB
11library. Users can create hash, btree or record based library files
12using the appropriate open call. Bsddb objects behave generally like
13dictionaries. Keys and values must be strings, however, so to use
14other objects as keys or to store other kinds of objects the user must
15serialize them somehow, typically using marshal.dumps or pickle.dumps.
Fred Drake9d158811999-04-19 21:19:21 +000016
Fred Drake6e5184f2000-09-15 15:19:35 +000017There are two incompatible versions of the underlying library.
Fred Drake38e5d272000-04-03 20:13:55 +000018Version 1.85 is widely available, but has some known bugs. Version 2
19is not quite as widely used, but does offer some improvements. The
Fred Drake6e5184f2000-09-15 15:19:35 +000020\module{bsddb} module uses the 1.85 interface. Starting with Python
212.0, the \program{configure} script can usually determine the
22version of the library which is available and build it correctly. If
23you have difficulty getting \program{configure} to do the right thing,
24run it with the \longprogramopt{help} option to get information about
25additional options that can help. On Windows, you will need to define
26the \code{HAVE_DB_185_H} macro if you are building Python from source
27and using version 2 of the DB library.
Fred Drake9d158811999-04-19 21:19:21 +000028
29The \module{bsddb} module defines the following functions that create
Fred Drake38e5d272000-04-03 20:13:55 +000030objects that access the appropriate type of Berkeley DB file. The
31first two arguments of each function are the same. For ease of
32portability, only the first two arguments should be used in most
33instances.
Fred Drake9d158811999-04-19 21:19:21 +000034
35\begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,
Fred Drake38e5d272000-04-03 20:13:55 +000036 mode\optional{, bsize\optional{,
37 ffactor\optional{, nelem\optional{,
38 cachesize\optional{, hash\optional{,
39 lorder}}}}}}}}}
40Open the hash format file named \var{filename}. The optional
41\var{flag} identifies the mode used to open the file. It may be
42\character{r} (read only), \character{w} (read-write),
43\character{c} (read-write - create if necessary) or
44\character{n} (read-write - truncate to zero length). The other
45arguments are rarely used and are just passed to the low-level
46\cfunction{dbopen()} function. Consult the Berkeley DB documentation
47for their use and interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000048\end{funcdesc}
49
Fred Drake9d158811999-04-19 21:19:21 +000050\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
51mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
52minkeypage\optional{, psize\optional{, lorder}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000053
54Open the btree format file named \var{filename}. The optional
55\var{flag} identifies the mode used to open the file. It may be
56\character{r} (read only), \character{w} (read-write),
57\character{c} (read-write - create if necessary) or
58\character{n} (read-write - truncate to zero length). The other
59arguments are rarely used and are just passed to the low-level dbopen
60function. Consult the Berkeley DB documentation for their use and
61interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000062\end{funcdesc}
63
64\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
65rnflags\optional{, cachesize\optional{, psize\optional{, lorder\optional{,
66reclen\optional{, bval\optional{, bfname}}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000067
68Open a DB record format file named \var{filename}. The optional
69\var{flag} identifies the mode used to open the file. It may be
70\character{r} (read only), \character{w} (read-write),
71\character{c} (read-write - create if necessary) or
72\character{n} (read-write - truncate to zero length). The other
73arguments are rarely used and are just passed to the low-level dbopen
74function. Consult the Berkeley DB documentation for their use and
75interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000076\end{funcdesc}
77
78
79\begin{seealso}
80 \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
81\end{seealso}
82
83
84\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
85
86Once instantiated, hash, btree and record objects support the following
87methods:
88
89\begin{methoddesc}{close}{}
90Close the underlying file. The object can no longer be accessed. Since
91there is no open \method{open} method for these objects, to open the file
92again a new \module{bsddb} module open function must be called.
93\end{methoddesc}
94
95\begin{methoddesc}{keys}{}
96Return the list of keys contained in the DB file. The order of the list is
97unspecified and should not be relied on. In particular, the order of the
98list returned is different for different file formats.
99\end{methoddesc}
100
101\begin{methoddesc}{has_key}{key}
Fred Drake38e5d272000-04-03 20:13:55 +0000102Return \code{1} if the DB file contains the argument as a key.
Fred Drake9d158811999-04-19 21:19:21 +0000103\end{methoddesc}
104
105\begin{methoddesc}{set_location}{key}
Fred Drakee1d47152001-01-05 06:44:19 +0000106Set the cursor to the item indicated by \var{key} and return a tuple
107containing the key and its value. For binary tree databases (opened
108using \function{btopen()}), if \var{key} does not actually exist in
109the database, the cursor will point to the next item in sorted order
110and return that key and value. For other databases,
111\exception{KeyError} will be raised if \var{key} is not found in the
112database.
Fred Drake9d158811999-04-19 21:19:21 +0000113\end{methoddesc}
114
115\begin{methoddesc}{first}{}
116Set the cursor to the first item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000117keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000118\end{methoddesc}
119
120\begin{methoddesc}{next}{}
121Set the cursor to the next item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000122keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000123\end{methoddesc}
124
125\begin{methoddesc}{previous}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000126Set the cursor to the first item in the DB file and return it. The
Fred Drake29cf6821999-04-23 20:32:59 +0000127order of keys in the file is unspecified, except in the case of B-Tree
128databases. This is not supported on hashtable databases (those opened
129with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000130\end{methoddesc}
131
132\begin{methoddesc}{last}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000133Set the cursor to the last item in the DB file and return it. The
134order of keys in the file is unspecified. This is not supported on
135hashtable databases (those opened with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000136\end{methoddesc}
137
138\begin{methoddesc}{sync}{}
139Synchronize the database on disk.
140\end{methoddesc}
141
142Example:
143
144\begin{verbatim}
145>>> import bsddb
146>>> db = bsddb.btopen('/tmp/spam.db', 'c')
147>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
148...
149>>> db['3']
150'9'
151>>> db.keys()
152['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
153>>> db.first()
154('0', '0')
155>>> db.next()
156('1', '1')
157>>> db.last()
158('9', '81')
159>>> db.set_location('2')
160('2', '4')
161>>> db.previous()
162('1', '1')
163>>> db.sync()
1640
165\end{verbatim}