blob: ff3d8e1ff2189f814112e928b91b4a4d178fcf96 [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}}}}}}}}}
Anthony Baxter83888952002-04-23 02:11:05 +000040Open the hash format file named \var{filename}. Files never intended
41to be preserved on disk may be created by passing \code{None} as the
42\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000043\var{flag} identifies the mode used to open the file. It may be
44\character{r} (read only), \character{w} (read-write),
45\character{c} (read-write - create if necessary) or
46\character{n} (read-write - truncate to zero length). The other
47arguments are rarely used and are just passed to the low-level
48\cfunction{dbopen()} function. Consult the Berkeley DB documentation
49for their use and interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000050\end{funcdesc}
51
Fred Drake9d158811999-04-19 21:19:21 +000052\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
53mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
54minkeypage\optional{, psize\optional{, lorder}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000055
Anthony Baxter83888952002-04-23 02:11:05 +000056Open the btree format file named \var{filename}. Files never intended
57to be preserved on disk may be created by passing \code{None} as the
58\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000059\var{flag} identifies the mode used to open the file. It may be
60\character{r} (read only), \character{w} (read-write),
61\character{c} (read-write - create if necessary) or
62\character{n} (read-write - truncate to zero length). The other
63arguments are rarely used and are just passed to the low-level dbopen
64function. Consult the Berkeley DB documentation for their use and
65interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000066\end{funcdesc}
67
68\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
69rnflags\optional{, cachesize\optional{, psize\optional{, lorder\optional{,
70reclen\optional{, bval\optional{, bfname}}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000071
Anthony Baxter83888952002-04-23 02:11:05 +000072Open a DB record format file named \var{filename}. Files never intended
73to be preserved on disk may be created by passing \code{None} as the
74\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000075\var{flag} identifies the mode used to open the file. It may be
76\character{r} (read only), \character{w} (read-write),
77\character{c} (read-write - create if necessary) or
78\character{n} (read-write - truncate to zero length). The other
79arguments are rarely used and are just passed to the low-level dbopen
80function. Consult the Berkeley DB documentation for their use and
81interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000082\end{funcdesc}
83
84
85\begin{seealso}
86 \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
87\end{seealso}
88
89
90\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
91
92Once instantiated, hash, btree and record objects support the following
93methods:
94
95\begin{methoddesc}{close}{}
96Close the underlying file. The object can no longer be accessed. Since
97there is no open \method{open} method for these objects, to open the file
98again a new \module{bsddb} module open function must be called.
99\end{methoddesc}
100
101\begin{methoddesc}{keys}{}
102Return the list of keys contained in the DB file. The order of the list is
103unspecified and should not be relied on. In particular, the order of the
104list returned is different for different file formats.
105\end{methoddesc}
106
107\begin{methoddesc}{has_key}{key}
Fred Drake38e5d272000-04-03 20:13:55 +0000108Return \code{1} if the DB file contains the argument as a key.
Fred Drake9d158811999-04-19 21:19:21 +0000109\end{methoddesc}
110
111\begin{methoddesc}{set_location}{key}
Fred Drakee1d47152001-01-05 06:44:19 +0000112Set the cursor to the item indicated by \var{key} and return a tuple
113containing the key and its value. For binary tree databases (opened
114using \function{btopen()}), if \var{key} does not actually exist in
115the database, the cursor will point to the next item in sorted order
116and return that key and value. For other databases,
117\exception{KeyError} will be raised if \var{key} is not found in the
118database.
Fred Drake9d158811999-04-19 21:19:21 +0000119\end{methoddesc}
120
121\begin{methoddesc}{first}{}
122Set the cursor to the first item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000123keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000124\end{methoddesc}
125
126\begin{methoddesc}{next}{}
127Set the cursor to the next item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000128keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000129\end{methoddesc}
130
131\begin{methoddesc}{previous}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000132Set the cursor to the first item in the DB file and return it. The
Fred Drake29cf6821999-04-23 20:32:59 +0000133order of keys in the file is unspecified, except in the case of B-Tree
134databases. This is not supported on hashtable databases (those opened
135with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000136\end{methoddesc}
137
138\begin{methoddesc}{last}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000139Set the cursor to the last item in the DB file and return it. The
140order of keys in the file is unspecified. This is not supported on
141hashtable databases (those opened with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000142\end{methoddesc}
143
144\begin{methoddesc}{sync}{}
145Synchronize the database on disk.
146\end{methoddesc}
147
148Example:
149
150\begin{verbatim}
151>>> import bsddb
152>>> db = bsddb.btopen('/tmp/spam.db', 'c')
153>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
154...
155>>> db['3']
156'9'
157>>> db.keys()
158['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
159>>> db.first()
160('0', '0')
161>>> db.next()
162('1', '1')
163>>> db.last()
164('9', '81')
165>>> db.set_location('2')
166('2', '4')
167>>> db.previous()
168('1', '1')
169>>> db.sync()
1700
171\end{verbatim}