blob: 6a27a3b8527387e6cf8408f6accac63852ae0174 [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 Drake38e5d272000-04-03 20:13:55 +000017The \module{bsddb} module is only available on \UNIX{} systems, so it
18is not built by default in the standard Python distribution. Also,
19there are two incompatible versions of the underlying library.
20Version 1.85 is widely available, but has some known bugs. Version 2
21is not quite as widely used, but does offer some improvements. The
22\module{bsddb} module uses the 1.85 interface. Users wishing to use
23version 2 of the Berkeley DB library will have to modify the source
24for the module to include \file{db_185.h} instead of
25\file{db.h} (\file{db_185.h} contains the version 1.85 compatibility
26interface).
Fred Drake9d158811999-04-19 21:19:21 +000027
28The \module{bsddb} module defines the following functions that create
Fred Drake38e5d272000-04-03 20:13:55 +000029objects that access the appropriate type of Berkeley DB file. The
30first two arguments of each function are the same. For ease of
31portability, only the first two arguments should be used in most
32instances.
Fred Drake9d158811999-04-19 21:19:21 +000033
34\begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,
Fred Drake38e5d272000-04-03 20:13:55 +000035 mode\optional{, bsize\optional{,
36 ffactor\optional{, nelem\optional{,
37 cachesize\optional{, hash\optional{,
38 lorder}}}}}}}}}
39Open the hash format file named \var{filename}. The optional
40\var{flag} identifies the mode used to open the file. It may be
41\character{r} (read only), \character{w} (read-write),
42\character{c} (read-write - create if necessary) or
43\character{n} (read-write - truncate to zero length). The other
44arguments are rarely used and are just passed to the low-level
45\cfunction{dbopen()} function. Consult the Berkeley DB documentation
46for their use and interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000047\end{funcdesc}
48
Fred Drake9d158811999-04-19 21:19:21 +000049\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
50mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
51minkeypage\optional{, psize\optional{, lorder}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000052
53Open the btree format file named \var{filename}. The optional
54\var{flag} identifies the mode used to open the file. It may be
55\character{r} (read only), \character{w} (read-write),
56\character{c} (read-write - create if necessary) or
57\character{n} (read-write - truncate to zero length). The other
58arguments are rarely used and are just passed to the low-level dbopen
59function. Consult the Berkeley DB documentation for their use and
60interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000061\end{funcdesc}
62
63\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
64rnflags\optional{, cachesize\optional{, psize\optional{, lorder\optional{,
65reclen\optional{, bval\optional{, bfname}}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000066
67Open a DB record format file named \var{filename}. The optional
68\var{flag} identifies the mode used to open the file. It may be
69\character{r} (read only), \character{w} (read-write),
70\character{c} (read-write - create if necessary) or
71\character{n} (read-write - truncate to zero length). The other
72arguments are rarely used and are just passed to the low-level dbopen
73function. Consult the Berkeley DB documentation for their use and
74interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000075\end{funcdesc}
76
77
78\begin{seealso}
79 \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
80\end{seealso}
81
82
83\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
84
85Once instantiated, hash, btree and record objects support the following
86methods:
87
88\begin{methoddesc}{close}{}
89Close the underlying file. The object can no longer be accessed. Since
90there is no open \method{open} method for these objects, to open the file
91again a new \module{bsddb} module open function must be called.
92\end{methoddesc}
93
94\begin{methoddesc}{keys}{}
95Return the list of keys contained in the DB file. The order of the list is
96unspecified and should not be relied on. In particular, the order of the
97list returned is different for different file formats.
98\end{methoddesc}
99
100\begin{methoddesc}{has_key}{key}
Fred Drake38e5d272000-04-03 20:13:55 +0000101Return \code{1} if the DB file contains the argument as a key.
Fred Drake9d158811999-04-19 21:19:21 +0000102\end{methoddesc}
103
104\begin{methoddesc}{set_location}{key}
105Set the cursor to the item indicated by the key and return it.
106\end{methoddesc}
107
108\begin{methoddesc}{first}{}
109Set the cursor to the first item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000110keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000111\end{methoddesc}
112
113\begin{methoddesc}{next}{}
114Set the cursor to the next item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000115keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000116\end{methoddesc}
117
118\begin{methoddesc}{previous}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000119Set the cursor to the first item in the DB file and return it. The
Fred Drake29cf6821999-04-23 20:32:59 +0000120order of keys in the file is unspecified, except in the case of B-Tree
121databases. This is not supported on hashtable databases (those opened
122with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000123\end{methoddesc}
124
125\begin{methoddesc}{last}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000126Set the cursor to the last item in the DB file and return it. The
127order of keys in the file is unspecified. This is not supported on
128hashtable databases (those opened with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000129\end{methoddesc}
130
131\begin{methoddesc}{sync}{}
132Synchronize the database on disk.
133\end{methoddesc}
134
135Example:
136
137\begin{verbatim}
138>>> import bsddb
139>>> db = bsddb.btopen('/tmp/spam.db', 'c')
140>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
141...
142>>> db['3']
143'9'
144>>> db.keys()
145['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
146>>> db.first()
147('0', '0')
148>>> db.next()
149('1', '1')
150>>> db.last()
151('9', '81')
152>>> db.set_location('2')
153('2', '4')
154>>> db.previous()
155('1', '1')
156>>> db.sync()
1570
158\end{verbatim}