blob: 29cecec8f306dfe47532b8ae9e583365567e1287 [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
Skip Montanaro6d9f45b2003-05-06 20:40:17 +000089\begin{notice}
90Beginning in 2.3 some Unix versions of Python may have a \module{bsddb185}
91module. This is present \emph{only} to allow backwards compatibility with
92systems which ship with the old Berkeley DB 1.85 database library. The
93\module{bsddb185} module should never be used directly in new code.
94\end{notice}
Fred Drake9d158811999-04-19 21:19:21 +000095
96\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
97
98Once instantiated, hash, btree and record objects support the following
99methods:
100
101\begin{methoddesc}{close}{}
102Close the underlying file. The object can no longer be accessed. Since
103there is no open \method{open} method for these objects, to open the file
104again a new \module{bsddb} module open function must be called.
105\end{methoddesc}
106
107\begin{methoddesc}{keys}{}
108Return the list of keys contained in the DB file. The order of the list is
109unspecified and should not be relied on. In particular, the order of the
110list returned is different for different file formats.
111\end{methoddesc}
112
113\begin{methoddesc}{has_key}{key}
Fred Drake38e5d272000-04-03 20:13:55 +0000114Return \code{1} if the DB file contains the argument as a key.
Fred Drake9d158811999-04-19 21:19:21 +0000115\end{methoddesc}
116
117\begin{methoddesc}{set_location}{key}
Fred Drakee1d47152001-01-05 06:44:19 +0000118Set the cursor to the item indicated by \var{key} and return a tuple
119containing the key and its value. For binary tree databases (opened
120using \function{btopen()}), if \var{key} does not actually exist in
121the database, the cursor will point to the next item in sorted order
122and return that key and value. For other databases,
123\exception{KeyError} will be raised if \var{key} is not found in the
124database.
Fred Drake9d158811999-04-19 21:19:21 +0000125\end{methoddesc}
126
127\begin{methoddesc}{first}{}
128Set the cursor to the first item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000129keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000130\end{methoddesc}
131
132\begin{methoddesc}{next}{}
133Set the cursor to the next item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000134keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000135\end{methoddesc}
136
137\begin{methoddesc}{previous}{}
Skip Montanaro61418122002-11-17 11:09:50 +0000138Set the cursor to the previous item in the DB file and return it. The
Fred Drake29cf6821999-04-23 20:32:59 +0000139order of keys in the file is unspecified, except in the case of B-Tree
140databases. This is not supported on hashtable databases (those opened
141with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000142\end{methoddesc}
143
144\begin{methoddesc}{last}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000145Set the cursor to the last item in the DB file and return it. The
146order of keys in the file is unspecified. This is not supported on
147hashtable databases (those opened with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000148\end{methoddesc}
149
150\begin{methoddesc}{sync}{}
151Synchronize the database on disk.
152\end{methoddesc}
153
154Example:
155
156\begin{verbatim}
157>>> import bsddb
158>>> db = bsddb.btopen('/tmp/spam.db', 'c')
159>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
160...
161>>> db['3']
162'9'
163>>> db.keys()
164['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
165>>> db.first()
166('0', '0')
167>>> db.next()
168('1', '1')
169>>> db.last()
170('9', '81')
171>>> db.set_location('2')
172('2', '4')
173>>> db.previous()
174('1', '1')
175>>> db.sync()
1760
177\end{verbatim}