source: vendor/python/2.5/Doc/lib/libbsddb.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 7.7 KB
Line 
1\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
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 \function{marshal.dumps()} or
16\function{pickle.dumps()}.
17
18The \module{bsddb} module requires a Berkeley DB library version from
193.3 thru 4.4.
20
21\begin{seealso}
22 \seeurl{http://pybsddb.sourceforge.net/}{The website with documentation
23 for the \module{bsddb.db} python Berkeley DB interface that closely mirrors
24 the Sleepycat object oriented interface provided in Berkeley DB 3 and 4.}
25 \seeurl{http://www.sleepycat.com/}{Sleepycat Software produces the
26 Berkeley DB library.}
27\end{seealso}
28
29A more modern DB, DBEnv and DBSequence object interface is available in the
30\module{bsddb.db} module which closely matches the Sleepycat Berkeley DB C API
31documented at the above URLs. Additional features provided by the
32\module{bsddb.db} API include fine tuning, transactions, logging, and
33multiprocess concurrent database access.
34
35The following is a description of the legacy \module{bsddb} interface
36compatible with the old python bsddb module. Starting in Python 2.5 this
37interface should be safe for multithreaded access. The \module{bsddb.db}
38API is recommended for threading users as it provides better control.
39
40The \module{bsddb} module defines the following functions that create
41objects that access the appropriate type of Berkeley DB file. The
42first two arguments of each function are the same. For ease of
43portability, only the first two arguments should be used in most
44instances.
45
46\begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,
47 mode\optional{, bsize\optional{,
48 ffactor\optional{, nelem\optional{,
49 cachesize\optional{, hash\optional{,
50 lorder}}}}}}}}}
51Open the hash format file named \var{filename}. Files never intended
52to be preserved on disk may be created by passing \code{None} as the
53\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; the default) or
57\character{n} (read-write - truncate to zero length). The other
58arguments are rarely used and are just passed to the low-level
59\cfunction{dbopen()} function. Consult the Berkeley DB documentation
60for their use and interpretation.
61\end{funcdesc}
62
63\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
64mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
65minkeypage\optional{, pgsize\optional{, lorder}}}}}}}}}
66
67Open the btree format file named \var{filename}. Files never intended
68to be preserved on disk may be created by passing \code{None} as the
69\var{filename}. The optional
70\var{flag} identifies the mode used to open the file. It may be
71\character{r} (read only), \character{w} (read-write),
72\character{c} (read-write - create if necessary; the default) or
73\character{n} (read-write - truncate to zero length). The other
74arguments are rarely used and are just passed to the low-level dbopen
75function. Consult the Berkeley DB documentation for their use and
76interpretation.
77\end{funcdesc}
78
79\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
80rnflags\optional{, cachesize\optional{, pgsize\optional{, lorder\optional{,
81reclen\optional{, bval\optional{, bfname}}}}}}}}}}
82
83Open a DB record format file named \var{filename}. Files never intended
84to be preserved on disk may be created by passing \code{None} as the
85\var{filename}. The optional
86\var{flag} identifies the mode used to open the file. It may be
87\character{r} (read only), \character{w} (read-write),
88\character{c} (read-write - create if necessary; the default) or
89\character{n} (read-write - truncate to zero length). The other
90arguments are rarely used and are just passed to the low-level dbopen
91function. Consult the Berkeley DB documentation for their use and
92interpretation.
93\end{funcdesc}
94
95
96\begin{notice}
97Beginning in 2.3 some \UNIX{} versions of Python may have a \module{bsddb185}
98module. This is present \emph{only} to allow backwards compatibility with
99systems which ship with the old Berkeley DB 1.85 database library. The
100\module{bsddb185} module should never be used directly in new code.
101\end{notice}
102
103
104\begin{seealso}
105 \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
106\end{seealso}
107
108\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
109
110Once instantiated, hash, btree and record objects support
111the same methods as dictionaries. In addition, they support
112the methods listed below.
113\versionchanged[Added dictionary methods]{2.3.1}
114
115\begin{methoddesc}{close}{}
116Close the underlying file. The object can no longer be accessed. Since
117there is no open \method{open} method for these objects, to open the file
118again a new \module{bsddb} module open function must be called.
119\end{methoddesc}
120
121\begin{methoddesc}{keys}{}
122Return the list of keys contained in the DB file. The order of the list is
123unspecified and should not be relied on. In particular, the order of the
124list returned is different for different file formats.
125\end{methoddesc}
126
127\begin{methoddesc}{has_key}{key}
128Return \code{1} if the DB file contains the argument as a key.
129\end{methoddesc}
130
131\begin{methoddesc}{set_location}{key}
132Set the cursor to the item indicated by \var{key} and return a tuple
133containing the key and its value. For binary tree databases (opened
134using \function{btopen()}), if \var{key} does not actually exist in
135the database, the cursor will point to the next item in sorted order
136and return that key and value. For other databases,
137\exception{KeyError} will be raised if \var{key} is not found in the
138database.
139\end{methoddesc}
140
141\begin{methoddesc}{first}{}
142Set the cursor to the first item in the DB file and return it. The order of
143keys in the file is unspecified, except in the case of B-Tree databases.
144This method raises \exception{bsddb.error} if the database is empty.
145\end{methoddesc}
146
147\begin{methoddesc}{next}{}
148Set the cursor to the next item in the DB file and return it. The order of
149keys in the file is unspecified, except in the case of B-Tree databases.
150\end{methoddesc}
151
152\begin{methoddesc}{previous}{}
153Set the cursor to the previous item in the DB file and return it. The
154order of keys in the file is unspecified, except in the case of B-Tree
155databases. This is not supported on hashtable databases (those opened
156with \function{hashopen()}).
157\end{methoddesc}
158
159\begin{methoddesc}{last}{}
160Set the cursor to the last item in the DB file and return it. The
161order of keys in the file is unspecified. This is not supported on
162hashtable databases (those opened with \function{hashopen()}).
163This method raises \exception{bsddb.error} if the database is empty.
164\end{methoddesc}
165
166\begin{methoddesc}{sync}{}
167Synchronize the database on disk.
168\end{methoddesc}
169
170Example:
171
172\begin{verbatim}
173>>> import bsddb
174>>> db = bsddb.btopen('/tmp/spam.db', 'c')
175>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
176...
177>>> db['3']
178'9'
179>>> db.keys()
180['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
181>>> db.first()
182('0', '0')
183>>> db.next()
184('1', '1')
185>>> db.last()
186('9', '81')
187>>> db.set_location('2')
188('2', '4')
189>>> db.previous()
190('1', '1')
191>>> for k, v in db.iteritems():
192... print k, v
1930 0
1941 1
1952 4
1963 9
1974 16
1985 25
1996 36
2007 49
2018 64
2029 81
203>>> '8' in db
204True
205>>> db.sync()
2060
207\end{verbatim}
Note: See TracBrowser for help on using the repository browser.