blob: 719783b63c1bb894d492884feb49111e52546424 [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
Gregory P. Smith57725132003-05-28 07:56:45 +000021\begin{seealso}
22 \seeurl{http://pybsddb.sourceforge.net/}{Website with documentation
23 for the new python Berkeley DB interface that closely mirrors the
24 sleepycat object oriented interface provided in Berkeley DB 3 and 4.}
25 \seeurl{http://www.sleepycat.com/}{Sleepycat Software produces the
Gregory P. Smithdad58602003-05-28 16:20:03 +000026 modern Berkeley DB library.}
Gregory P. Smith57725132003-05-28 07:56:45 +000027\end{seealso}
28
29The following is a description of the legacy \module{bsddb} interface
30compatible with the old python bsddb module. For details about the more
31modern Db and DbEnv object oriented interface see the above mentioned
32pybsddb URL.
Fred Drake9d158811999-04-19 21:19:21 +000033
34The \module{bsddb} module defines the following functions that create
Fred Drake38e5d272000-04-03 20:13:55 +000035objects that access the appropriate type of Berkeley DB file. The
36first two arguments of each function are the same. For ease of
37portability, only the first two arguments should be used in most
38instances.
Fred Drake9d158811999-04-19 21:19:21 +000039
40\begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,
Fred Drake38e5d272000-04-03 20:13:55 +000041 mode\optional{, bsize\optional{,
42 ffactor\optional{, nelem\optional{,
43 cachesize\optional{, hash\optional{,
44 lorder}}}}}}}}}
Anthony Baxter83888952002-04-23 02:11:05 +000045Open the hash format file named \var{filename}. Files never intended
46to be preserved on disk may be created by passing \code{None} as the
47\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000048\var{flag} identifies the mode used to open the file. It may be
Gregory P. Smith57725132003-05-28 07:56:45 +000049\character{r} (read only, default), \character{w} (read-write) ,
Fred Drake38e5d272000-04-03 20:13:55 +000050\character{c} (read-write - create if necessary) or
51\character{n} (read-write - truncate to zero length). The other
52arguments are rarely used and are just passed to the low-level
53\cfunction{dbopen()} function. Consult the Berkeley DB documentation
54for their use and interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000055\end{funcdesc}
56
Fred Drake9d158811999-04-19 21:19:21 +000057\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
58mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
59minkeypage\optional{, psize\optional{, lorder}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000060
Anthony Baxter83888952002-04-23 02:11:05 +000061Open the btree format file named \var{filename}. Files never intended
62to be preserved on disk may be created by passing \code{None} as the
63\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000064\var{flag} identifies the mode used to open the file. It may be
Gregory P. Smith57725132003-05-28 07:56:45 +000065\character{r} (read only, default), \character{w} (read-write),
Fred Drake38e5d272000-04-03 20:13:55 +000066\character{c} (read-write - create if necessary) or
67\character{n} (read-write - truncate to zero length). The other
68arguments are rarely used and are just passed to the low-level dbopen
69function. Consult the Berkeley DB documentation for their use and
70interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000071\end{funcdesc}
72
73\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
74rnflags\optional{, cachesize\optional{, psize\optional{, lorder\optional{,
75reclen\optional{, bval\optional{, bfname}}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000076
Anthony Baxter83888952002-04-23 02:11:05 +000077Open a DB record format file named \var{filename}. Files never intended
78to be preserved on disk may be created by passing \code{None} as the
79\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000080\var{flag} identifies the mode used to open the file. It may be
Gregory P. Smith57725132003-05-28 07:56:45 +000081\character{r} (read only, default), \character{w} (read-write),
Fred Drake38e5d272000-04-03 20:13:55 +000082\character{c} (read-write - create if necessary) or
83\character{n} (read-write - truncate to zero length). The other
84arguments are rarely used and are just passed to the low-level dbopen
85function. Consult the Berkeley DB documentation for their use and
86interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000087\end{funcdesc}
88
89
90\begin{seealso}
91 \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
92\end{seealso}
93
Skip Montanaro6d9f45b2003-05-06 20:40:17 +000094\begin{notice}
95Beginning in 2.3 some Unix versions of Python may have a \module{bsddb185}
96module. This is present \emph{only} to allow backwards compatibility with
97systems which ship with the old Berkeley DB 1.85 database library. The
98\module{bsddb185} module should never be used directly in new code.
99\end{notice}
Fred Drake9d158811999-04-19 21:19:21 +0000100
101\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
102
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000103Once instantiated, hash, btree and record objects support
104the same methods as dictionaries. In addition, they support
105the following methods:
Fred Drake9d158811999-04-19 21:19:21 +0000106
107\begin{methoddesc}{close}{}
108Close the underlying file. The object can no longer be accessed. Since
109there is no open \method{open} method for these objects, to open the file
110again a new \module{bsddb} module open function must be called.
111\end{methoddesc}
112
113\begin{methoddesc}{keys}{}
114Return the list of keys contained in the DB file. The order of the list is
115unspecified and should not be relied on. In particular, the order of the
116list returned is different for different file formats.
117\end{methoddesc}
118
119\begin{methoddesc}{has_key}{key}
Fred Drake38e5d272000-04-03 20:13:55 +0000120Return \code{1} if the DB file contains the argument as a key.
Fred Drake9d158811999-04-19 21:19:21 +0000121\end{methoddesc}
122
123\begin{methoddesc}{set_location}{key}
Fred Drakee1d47152001-01-05 06:44:19 +0000124Set the cursor to the item indicated by \var{key} and return a tuple
125containing the key and its value. For binary tree databases (opened
126using \function{btopen()}), if \var{key} does not actually exist in
127the database, the cursor will point to the next item in sorted order
128and return that key and value. For other databases,
129\exception{KeyError} will be raised if \var{key} is not found in the
130database.
Fred Drake9d158811999-04-19 21:19:21 +0000131\end{methoddesc}
132
133\begin{methoddesc}{first}{}
134Set the cursor to the first item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000135keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000136\end{methoddesc}
137
138\begin{methoddesc}{next}{}
139Set the cursor to the next 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}{previous}{}
Skip Montanaro61418122002-11-17 11:09:50 +0000144Set the cursor to the previous item in the DB file and return it. The
Fred Drake29cf6821999-04-23 20:32:59 +0000145order of keys in the file is unspecified, except in the case of B-Tree
146databases. This is not supported on hashtable databases (those opened
147with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000148\end{methoddesc}
149
150\begin{methoddesc}{last}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000151Set the cursor to the last item in the DB file and return it. The
152order of keys in the file is unspecified. This is not supported on
153hashtable databases (those opened with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000154\end{methoddesc}
155
156\begin{methoddesc}{sync}{}
157Synchronize the database on disk.
158\end{methoddesc}
159
160Example:
161
162\begin{verbatim}
163>>> import bsddb
164>>> db = bsddb.btopen('/tmp/spam.db', 'c')
165>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
166...
167>>> db['3']
168'9'
169>>> db.keys()
170['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
171>>> db.first()
172('0', '0')
173>>> db.next()
174('1', '1')
175>>> db.last()
176('9', '81')
177>>> db.set_location('2')
178('2', '4')
179>>> db.previous()
180('1', '1')
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000181>>> for k, v in db.iteritems():
182... print k, v
1830 0
1841 1
1852 4
1863 9
1874 16
1885 25
1896 36
1907 49
1918 64
1929 81
193>>> 8 in db
194True
Fred Drake9d158811999-04-19 21:19:21 +0000195>>> db.sync()
1960
197\end{verbatim}