blob: 385f558728b1ded1b24e11df77c98feaa8f9ffdb [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
10The \module{bsddb} module provides an interface to the Berkeley DB library.
11Users can create hash, btree or record based library files using the
12appropriate open call. Bsddb objects behave generally like dictionaries.
13Keys and values must be strings, however, so to use other objects as keys or
14to store other kinds of objects the user must serialize them somehow,
15typically using marshal.dumps or pickle.dumps.
16
17The \module{bsddb} module is only available on \UNIX{} systems, so it is not
18built by default in the standard Python distribution. Also, there are two
19incompatible versions of the underlying library. Version 1.85 is widely
20available, but has some known bugs. Version 2 is not quite as widely used,
21but does offer some improvements. The \module{bsddb} module uses the 1.85
22interface. Users wishing to use version 2 of the Berkeley DB library will
23have to modify the source for the module to include db_185.h instead of
24db.h.
25
26The \module{bsddb} module defines the following functions that create
27objects that access the appropriate type of Berkeley DB file. The first two
28arguments of each function are the same. For ease of portability, only the
29first two arguments should be used in most instances.
30
31\begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,
32mode\optional{, bsize\optional{, ffactor\optional{, nelem\optional{,
33cachesize\optional{, hash\optional{, lorder}}}}}}}}}
34Open the hash format file named \var{filename}. The optional \var{flag}
35identifies the mode used to open the file. It may be ``r'' (read only),
36``w'' (read-write), ``c'' (read-write - create if necessary) or ``n''
37(read-write - truncate to zero length). The other arguments are rarely used
38and are just passed to the low-level dbopen function. Consult the
39Berkeley DB documentation for their use and interpretation.
40\end{funcdesc}
41
42
43\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
44mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
45minkeypage\optional{, psize\optional{, lorder}}}}}}}}}
46Open the btree format file named \var{filename}. The optional \var{flag}
47identifies the mode used to open the file. It may be ``r'' (read only),
48``w'' (read-write), ``c'' (read-write - create if necessary) or ``n''
49(read-write - truncate to zero length). The other arguments are rarely used
50and are just passed to the low-level dbopen function. Consult the
51Berkeley DB documentation for their use and interpretation.
52\end{funcdesc}
53
54\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
55rnflags\optional{, cachesize\optional{, psize\optional{, lorder\optional{,
56reclen\optional{, bval\optional{, bfname}}}}}}}}}}
57Open a DB record format file named \var{filename}. The optional \var{flag}
58identifies the mode used to open the file. It may be ``r'' (read only),
59``w'' (read-write), ``c'' (read-write - create if necessary) or ``n''
60(read-write - truncate to zero length). The other arguments are rarely used
61and are just passed to the low-level dbopen function. Consult the
62Berkeley DB documentation for their use and interpretation.
63\end{funcdesc}
64
65
66\begin{seealso}
67 \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
68\end{seealso}
69
70
71\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
72
73Once instantiated, hash, btree and record objects support the following
74methods:
75
76\begin{methoddesc}{close}{}
77Close the underlying file. The object can no longer be accessed. Since
78there is no open \method{open} method for these objects, to open the file
79again a new \module{bsddb} module open function must be called.
80\end{methoddesc}
81
82\begin{methoddesc}{keys}{}
83Return the list of keys contained in the DB file. The order of the list is
84unspecified and should not be relied on. In particular, the order of the
85list returned is different for different file formats.
86\end{methoddesc}
87
88\begin{methoddesc}{has_key}{key}
89Return 1 if the DB file contains the argument as a key.
90\end{methoddesc}
91
92\begin{methoddesc}{set_location}{key}
93Set the cursor to the item indicated by the key and return it.
94\end{methoddesc}
95
96\begin{methoddesc}{first}{}
97Set the cursor to the first item in the DB file and return it. The order of
98keys in the file is unspecified.
99\end{methoddesc}
100
101\begin{methoddesc}{next}{}
102Set the cursor to the next item in the DB file and return it. The order of
103keys in the file is unspecified.
104\end{methoddesc}
105
106\begin{methoddesc}{previous}{}
107Set the cursor to the first item in the DB file and return it. The order of
108keys in the file is unspecified.
109\end{methoddesc}
110
111\begin{methoddesc}{last}{}
112Set the cursor to the last item in the DB file and return it. The order of
113keys in the file is unspecified.
114\end{methoddesc}
115
116\begin{methoddesc}{sync}{}
117Synchronize the database on disk.
118\end{methoddesc}
119
120Example:
121
122\begin{verbatim}
123>>> import bsddb
124>>> db = bsddb.btopen('/tmp/spam.db', 'c')
125>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
126...
127>>> db['3']
128'9'
129>>> db.keys()
130['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
131>>> db.first()
132('0', '0')
133>>> db.next()
134('1', '1')
135>>> db.last()
136('9', '81')
137>>> db.set_location('2')
138('2', '4')
139>>> db.previous()
140('1', '1')
141>>> db.sync()
1420
143\end{verbatim}