blob: be720c7dde5ed090e42d53c36cbe559de6bd8253 [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
Gregory P. Smith57725132003-05-28 07:56:45 +000017Starting with Python 2.3 the \module{bsddb} module requires the
18Berkeley DB library version 3.1 or later (it is known to work with 3.1
19thru 4.1 at the time of this writing).
20
21NOTE: BerkeleyDB has its own license which may apply to you if you
22intend to distribute your application outside of your organization in
23a non-opensource manner. Visit Sleepycat Software's website to read
24the full license.
25
26\begin{seealso}
27 \seeurl{http://pybsddb.sourceforge.net/}{Website with documentation
28 for the new python Berkeley DB interface that closely mirrors the
29 sleepycat object oriented interface provided in Berkeley DB 3 and 4.}
30 \seeurl{http://www.sleepycat.com/}{Sleepycat Software produces the
31 modern Berkeley DB library. They offer support and sell licenses for
32 proprietary software using BerkeleyDB.}
33\end{seealso}
34
35The following is a description of the legacy \module{bsddb} interface
36compatible with the old python bsddb module. For details about the more
37modern Db and DbEnv object oriented interface see the above mentioned
38pybsddb URL.
Fred Drake9d158811999-04-19 21:19:21 +000039
40The \module{bsddb} module defines the following functions that create
Fred Drake38e5d272000-04-03 20:13:55 +000041objects 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.
Fred Drake9d158811999-04-19 21:19:21 +000045
46\begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,
Fred Drake38e5d272000-04-03 20:13:55 +000047 mode\optional{, bsize\optional{,
48 ffactor\optional{, nelem\optional{,
49 cachesize\optional{, hash\optional{,
50 lorder}}}}}}}}}
Anthony Baxter83888952002-04-23 02:11:05 +000051Open 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
Fred Drake38e5d272000-04-03 20:13:55 +000054\var{flag} identifies the mode used to open the file. It may be
Gregory P. Smith57725132003-05-28 07:56:45 +000055\character{r} (read only, default), \character{w} (read-write) ,
Fred Drake38e5d272000-04-03 20:13:55 +000056\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
59\cfunction{dbopen()} function. Consult the Berkeley DB documentation
60for their use and interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000061\end{funcdesc}
62
Fred Drake9d158811999-04-19 21:19:21 +000063\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
64mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
65minkeypage\optional{, psize\optional{, lorder}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000066
Anthony Baxter83888952002-04-23 02:11:05 +000067Open 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
Fred Drake38e5d272000-04-03 20:13:55 +000070\var{flag} identifies the mode used to open the file. It may be
Gregory P. Smith57725132003-05-28 07:56:45 +000071\character{r} (read only, default), \character{w} (read-write),
Fred Drake38e5d272000-04-03 20:13:55 +000072\character{c} (read-write - create if necessary) 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.
Fred Drake9d158811999-04-19 21:19:21 +000077\end{funcdesc}
78
79\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
80rnflags\optional{, cachesize\optional{, psize\optional{, lorder\optional{,
81reclen\optional{, bval\optional{, bfname}}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000082
Anthony Baxter83888952002-04-23 02:11:05 +000083Open 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
Fred Drake38e5d272000-04-03 20:13:55 +000086\var{flag} identifies the mode used to open the file. It may be
Gregory P. Smith57725132003-05-28 07:56:45 +000087\character{r} (read only, default), \character{w} (read-write),
Fred Drake38e5d272000-04-03 20:13:55 +000088\character{c} (read-write - create if necessary) 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.
Fred Drake9d158811999-04-19 21:19:21 +000093\end{funcdesc}
94
95
96\begin{seealso}
97 \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
98\end{seealso}
99
Skip Montanaro6d9f45b2003-05-06 20:40:17 +0000100\begin{notice}
101Beginning in 2.3 some Unix versions of Python may have a \module{bsddb185}
102module. This is present \emph{only} to allow backwards compatibility with
103systems which ship with the old Berkeley DB 1.85 database library. The
104\module{bsddb185} module should never be used directly in new code.
105\end{notice}
Fred Drake9d158811999-04-19 21:19:21 +0000106
107\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
108
109Once instantiated, hash, btree and record objects support the following
110methods:
111
112\begin{methoddesc}{close}{}
113Close the underlying file. The object can no longer be accessed. Since
114there is no open \method{open} method for these objects, to open the file
115again a new \module{bsddb} module open function must be called.
116\end{methoddesc}
117
118\begin{methoddesc}{keys}{}
119Return the list of keys contained in the DB file. The order of the list is
120unspecified and should not be relied on. In particular, the order of the
121list returned is different for different file formats.
122\end{methoddesc}
123
124\begin{methoddesc}{has_key}{key}
Fred Drake38e5d272000-04-03 20:13:55 +0000125Return \code{1} if the DB file contains the argument as a key.
Fred Drake9d158811999-04-19 21:19:21 +0000126\end{methoddesc}
127
128\begin{methoddesc}{set_location}{key}
Fred Drakee1d47152001-01-05 06:44:19 +0000129Set the cursor to the item indicated by \var{key} and return a tuple
130containing the key and its value. For binary tree databases (opened
131using \function{btopen()}), if \var{key} does not actually exist in
132the database, the cursor will point to the next item in sorted order
133and return that key and value. For other databases,
134\exception{KeyError} will be raised if \var{key} is not found in the
135database.
Fred Drake9d158811999-04-19 21:19:21 +0000136\end{methoddesc}
137
138\begin{methoddesc}{first}{}
139Set the cursor to the first item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000140keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000141\end{methoddesc}
142
143\begin{methoddesc}{next}{}
144Set the cursor to the next item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000145keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000146\end{methoddesc}
147
148\begin{methoddesc}{previous}{}
Skip Montanaro61418122002-11-17 11:09:50 +0000149Set the cursor to the previous item in the DB file and return it. The
Fred Drake29cf6821999-04-23 20:32:59 +0000150order of keys in the file is unspecified, except in the case of B-Tree
151databases. This is not supported on hashtable databases (those opened
152with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000153\end{methoddesc}
154
155\begin{methoddesc}{last}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000156Set the cursor to the last item in the DB file and return it. The
157order of keys in the file is unspecified. This is not supported on
158hashtable databases (those opened with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000159\end{methoddesc}
160
161\begin{methoddesc}{sync}{}
162Synchronize the database on disk.
163\end{methoddesc}
164
165Example:
166
167\begin{verbatim}
168>>> import bsddb
169>>> db = bsddb.btopen('/tmp/spam.db', 'c')
170>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
171...
172>>> db['3']
173'9'
174>>> db.keys()
175['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
176>>> db.first()
177('0', '0')
178>>> db.next()
179('1', '1')
180>>> db.last()
181('9', '81')
182>>> db.set_location('2')
183('2', '4')
184>>> db.previous()
185('1', '1')
186>>> db.sync()
1870
188\end{verbatim}