blob: fa7bb4b76570ad081fb61ccc7ae5f328e2509221 [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
Andrew M. Kuchling97473012005-12-22 20:12:54 +000015serialize them somehow, typically using \function{marshal.dumps()} or
16\function{pickle.dumps}.
Fred Drake9d158811999-04-19 21:19:21 +000017
Gregory P. Smith57725132003-05-28 07:56:45 +000018Starting with Python 2.3 the \module{bsddb} module requires the
Gregory P. Smithb845ef02003-11-13 08:30:03 +000019Berkeley DB library version 3.2 or later (it is known to work with 3.2
Raymond Hettinger68804312005-01-01 00:28:46 +000020through 4.3 at the time of this writing).
Gregory P. Smith57725132003-05-28 07:56:45 +000021
Gregory P. Smith57725132003-05-28 07:56:45 +000022\begin{seealso}
23 \seeurl{http://pybsddb.sourceforge.net/}{Website with documentation
24 for the new python Berkeley DB interface that closely mirrors the
25 sleepycat object oriented interface provided in Berkeley DB 3 and 4.}
26 \seeurl{http://www.sleepycat.com/}{Sleepycat Software produces the
Gregory P. Smithdad58602003-05-28 16:20:03 +000027 modern Berkeley DB library.}
Gregory P. Smith57725132003-05-28 07:56:45 +000028\end{seealso}
29
30The following is a description of the legacy \module{bsddb} interface
31compatible with the old python bsddb module. For details about the more
32modern Db and DbEnv object oriented interface see the above mentioned
33pybsddb URL.
Fred Drake9d158811999-04-19 21:19:21 +000034
35The \module{bsddb} module defines the following functions that create
Fred Drake38e5d272000-04-03 20:13:55 +000036objects that access the appropriate type of Berkeley DB file. The
37first two arguments of each function are the same. For ease of
38portability, only the first two arguments should be used in most
39instances.
Fred Drake9d158811999-04-19 21:19:21 +000040
41\begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,
Fred Drake38e5d272000-04-03 20:13:55 +000042 mode\optional{, bsize\optional{,
43 ffactor\optional{, nelem\optional{,
44 cachesize\optional{, hash\optional{,
45 lorder}}}}}}}}}
Anthony Baxter83888952002-04-23 02:11:05 +000046Open the hash format file named \var{filename}. Files never intended
47to be preserved on disk may be created by passing \code{None} as the
48\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000049\var{flag} identifies the mode used to open the file. It may be
Fred Drakefdccf1a2004-07-26 16:33:29 +000050\character{r} (read only), \character{w} (read-write) ,
51\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000052\character{n} (read-write - truncate to zero length). The other
53arguments are rarely used and are just passed to the low-level
54\cfunction{dbopen()} function. Consult the Berkeley DB documentation
55for their use and interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000056\end{funcdesc}
57
Fred Drake9d158811999-04-19 21:19:21 +000058\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
59mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
Andrew M. Kuchling8dbe1a72005-06-08 21:51:28 +000060minkeypage\optional{, pgsize\optional{, lorder}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000061
Anthony Baxter83888952002-04-23 02:11:05 +000062Open the btree format file named \var{filename}. Files never intended
63to be preserved on disk may be created by passing \code{None} as the
64\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000065\var{flag} identifies the mode used to open the file. It may be
Fred Drakefdccf1a2004-07-26 16:33:29 +000066\character{r} (read only), \character{w} (read-write),
67\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000068\character{n} (read-write - truncate to zero length). The other
69arguments are rarely used and are just passed to the low-level dbopen
70function. Consult the Berkeley DB documentation for their use and
71interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000072\end{funcdesc}
73
74\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
Andrew M. Kuchling8dbe1a72005-06-08 21:51:28 +000075rnflags\optional{, cachesize\optional{, pgsize\optional{, lorder\optional{,
Fred Drake9d158811999-04-19 21:19:21 +000076reclen\optional{, bval\optional{, bfname}}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000077
Anthony Baxter83888952002-04-23 02:11:05 +000078Open a DB record format file named \var{filename}. Files never intended
79to be preserved on disk may be created by passing \code{None} as the
80\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000081\var{flag} identifies the mode used to open the file. It may be
Fred Drakefdccf1a2004-07-26 16:33:29 +000082\character{r} (read only), \character{w} (read-write),
83\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000084\character{n} (read-write - truncate to zero length). The other
85arguments are rarely used and are just passed to the low-level dbopen
86function. Consult the Berkeley DB documentation for their use and
87interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000088\end{funcdesc}
89
90
Skip Montanaro6d9f45b2003-05-06 20:40:17 +000091\begin{notice}
92Beginning in 2.3 some Unix versions of Python may have a \module{bsddb185}
93module. This is present \emph{only} to allow backwards compatibility with
94systems which ship with the old Berkeley DB 1.85 database library. The
95\module{bsddb185} module should never be used directly in new code.
96\end{notice}
Fred Drake9d158811999-04-19 21:19:21 +000097
Fred Drakeb86aa992004-06-24 06:03:59 +000098
99\begin{seealso}
100 \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
101\end{seealso}
102
Fred Drake9d158811999-04-19 21:19:21 +0000103\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
104
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000105Once instantiated, hash, btree and record objects support
106the same methods as dictionaries. In addition, they support
Raymond Hettinger34040342003-09-16 21:45:22 +0000107the methods listed below.
108\versionchanged[Added dictionary methods]{2.3.1}
Fred Drake9d158811999-04-19 21:19:21 +0000109
110\begin{methoddesc}{close}{}
111Close the underlying file. The object can no longer be accessed. Since
112there is no open \method{open} method for these objects, to open the file
113again a new \module{bsddb} module open function must be called.
114\end{methoddesc}
115
116\begin{methoddesc}{keys}{}
117Return the list of keys contained in the DB file. The order of the list is
118unspecified and should not be relied on. In particular, the order of the
119list returned is different for different file formats.
120\end{methoddesc}
121
122\begin{methoddesc}{has_key}{key}
Fred Drake38e5d272000-04-03 20:13:55 +0000123Return \code{1} if the DB file contains the argument as a key.
Fred Drake9d158811999-04-19 21:19:21 +0000124\end{methoddesc}
125
126\begin{methoddesc}{set_location}{key}
Fred Drakee1d47152001-01-05 06:44:19 +0000127Set the cursor to the item indicated by \var{key} and return a tuple
128containing the key and its value. For binary tree databases (opened
129using \function{btopen()}), if \var{key} does not actually exist in
130the database, the cursor will point to the next item in sorted order
131and return that key and value. For other databases,
132\exception{KeyError} will be raised if \var{key} is not found in the
133database.
Fred Drake9d158811999-04-19 21:19:21 +0000134\end{methoddesc}
135
136\begin{methoddesc}{first}{}
137Set the cursor to the first item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000138keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drakeba100c92004-08-10 19:22:48 +0000139This method raises \exception{bsddb.error} if the database is empty.
Fred Drake9d158811999-04-19 21:19:21 +0000140\end{methoddesc}
141
142\begin{methoddesc}{next}{}
143Set the cursor to the next item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000144keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000145\end{methoddesc}
146
147\begin{methoddesc}{previous}{}
Skip Montanaro61418122002-11-17 11:09:50 +0000148Set the cursor to the previous item in the DB file and return it. The
Fred Drake29cf6821999-04-23 20:32:59 +0000149order of keys in the file is unspecified, except in the case of B-Tree
150databases. This is not supported on hashtable databases (those opened
151with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000152\end{methoddesc}
153
154\begin{methoddesc}{last}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000155Set the cursor to the last item in the DB file and return it. The
156order of keys in the file is unspecified. This is not supported on
157hashtable databases (those opened with \function{hashopen()}).
Fred Drakeba100c92004-08-10 19:22:48 +0000158This method raises \exception{bsddb.error} if the database is empty.
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')
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000186>>> for k, v in db.iteritems():
187... print k, v
1880 0
1891 1
1902 4
1913 9
1924 16
1935 25
1946 36
1957 49
1968 64
1979 81
Raymond Hettingerff294fe2003-12-07 13:00:25 +0000198>>> '8' in db
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000199True
Fred Drake9d158811999-04-19 21:19:21 +0000200>>> db.sync()
2010
202\end{verbatim}