blob: 85ea824db08c69148718fd9d3a0394628c272159 [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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000016\function{pickle.dumps()}.
Fred Drake9d158811999-04-19 21:19:21 +000017
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000018The \module{bsddb} module requires a Berkeley DB library version from
193.3 thru 4.4.
Gregory P. Smith57725132003-05-28 07:56:45 +000020
Gregory P. Smith57725132003-05-28 07:56:45 +000021\begin{seealso}
Thomas Wouters89f507f2006-12-13 04:49:30 +000022 \seeurl{http://pybsddb.sourceforge.net/}
23 {The website with documentation for the \module{bsddb.db}
24 Python Berkeley DB interface that closely mirrors the object
25 oriented interface provided in Berkeley DB 3 and 4.}
26
27 \seeurl{http://www.oracle.com/database/berkeley-db/}
28 {The Berkeley DB library.}
Gregory P. Smith57725132003-05-28 07:56:45 +000029\end{seealso}
30
Thomas Wouters0e3f5912006-08-11 14:57:12 +000031A more modern DB, DBEnv and DBSequence object interface is available in the
Thomas Wouters89f507f2006-12-13 04:49:30 +000032\module{bsddb.db} module which closely matches the Berkeley DB C API
Thomas Wouters0e3f5912006-08-11 14:57:12 +000033documented at the above URLs. Additional features provided by the
34\module{bsddb.db} API include fine tuning, transactions, logging, and
35multiprocess concurrent database access.
36
Gregory P. Smith57725132003-05-28 07:56:45 +000037The following is a description of the legacy \module{bsddb} interface
Thomas Wouters89f507f2006-12-13 04:49:30 +000038compatible with the old Python bsddb module. Starting in Python 2.5 this
Thomas Wouters0e3f5912006-08-11 14:57:12 +000039interface should be safe for multithreaded access. The \module{bsddb.db}
40API is recommended for threading users as it provides better control.
Fred Drake9d158811999-04-19 21:19:21 +000041
42The \module{bsddb} module defines the following functions that create
Fred Drake38e5d272000-04-03 20:13:55 +000043objects that access the appropriate type of Berkeley DB file. The
44first two arguments of each function are the same. For ease of
45portability, only the first two arguments should be used in most
46instances.
Fred Drake9d158811999-04-19 21:19:21 +000047
48\begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,
Fred Drake38e5d272000-04-03 20:13:55 +000049 mode\optional{, bsize\optional{,
50 ffactor\optional{, nelem\optional{,
51 cachesize\optional{, hash\optional{,
52 lorder}}}}}}}}}
Anthony Baxter83888952002-04-23 02:11:05 +000053Open the hash format file named \var{filename}. Files never intended
54to be preserved on disk may be created by passing \code{None} as the
55\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000056\var{flag} identifies the mode used to open the file. It may be
Fred Drakefdccf1a2004-07-26 16:33:29 +000057\character{r} (read only), \character{w} (read-write) ,
58\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000059\character{n} (read-write - truncate to zero length). The other
60arguments are rarely used and are just passed to the low-level
61\cfunction{dbopen()} function. Consult the Berkeley DB documentation
62for their use and interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000063\end{funcdesc}
64
Fred Drake9d158811999-04-19 21:19:21 +000065\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
66mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
Andrew M. Kuchling8dbe1a72005-06-08 21:51:28 +000067minkeypage\optional{, pgsize\optional{, lorder}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000068
Anthony Baxter83888952002-04-23 02:11:05 +000069Open the btree format file named \var{filename}. Files never intended
70to be preserved on disk may be created by passing \code{None} as the
71\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000072\var{flag} identifies the mode used to open the file. It may be
Fred Drakefdccf1a2004-07-26 16:33:29 +000073\character{r} (read only), \character{w} (read-write),
74\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000075\character{n} (read-write - truncate to zero length). The other
76arguments are rarely used and are just passed to the low-level dbopen
77function. Consult the Berkeley DB documentation for their use and
78interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000079\end{funcdesc}
80
81\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
Andrew M. Kuchling8dbe1a72005-06-08 21:51:28 +000082rnflags\optional{, cachesize\optional{, pgsize\optional{, lorder\optional{,
Fred Drake9d158811999-04-19 21:19:21 +000083reclen\optional{, bval\optional{, bfname}}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000084
Anthony Baxter83888952002-04-23 02:11:05 +000085Open a DB record format file named \var{filename}. Files never intended
86to be preserved on disk may be created by passing \code{None} as the
87\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000088\var{flag} identifies the mode used to open the file. It may be
Fred Drakefdccf1a2004-07-26 16:33:29 +000089\character{r} (read only), \character{w} (read-write),
90\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000091\character{n} (read-write - truncate to zero length). The other
92arguments are rarely used and are just passed to the low-level dbopen
93function. Consult the Berkeley DB documentation for their use and
94interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000095\end{funcdesc}
96
97
Skip Montanaro6d9f45b2003-05-06 20:40:17 +000098\begin{notice}
Thomas Wouters0e3f5912006-08-11 14:57:12 +000099Beginning in 2.3 some \UNIX{} versions of Python may have a \module{bsddb185}
Skip Montanaro6d9f45b2003-05-06 20:40:17 +0000100module. This is present \emph{only} to allow backwards compatibility with
101systems which ship with the old Berkeley DB 1.85 database library. The
102\module{bsddb185} module should never be used directly in new code.
103\end{notice}
Fred Drake9d158811999-04-19 21:19:21 +0000104
Fred Drakeb86aa992004-06-24 06:03:59 +0000105
106\begin{seealso}
107 \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
108\end{seealso}
109
Fred Drake9d158811999-04-19 21:19:21 +0000110\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
111
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000112Once instantiated, hash, btree and record objects support
113the same methods as dictionaries. In addition, they support
Raymond Hettinger34040342003-09-16 21:45:22 +0000114the methods listed below.
115\versionchanged[Added dictionary methods]{2.3.1}
Fred Drake9d158811999-04-19 21:19:21 +0000116
117\begin{methoddesc}{close}{}
118Close the underlying file. The object can no longer be accessed. Since
119there is no open \method{open} method for these objects, to open the file
120again a new \module{bsddb} module open function must be called.
121\end{methoddesc}
122
123\begin{methoddesc}{keys}{}
124Return the list of keys contained in the DB file. The order of the list is
125unspecified and should not be relied on. In particular, the order of the
126list returned is different for different file formats.
127\end{methoddesc}
128
129\begin{methoddesc}{has_key}{key}
Fred Drake38e5d272000-04-03 20:13:55 +0000130Return \code{1} if the DB file contains the argument as a key.
Fred Drake9d158811999-04-19 21:19:21 +0000131\end{methoddesc}
132
133\begin{methoddesc}{set_location}{key}
Fred Drakee1d47152001-01-05 06:44:19 +0000134Set the cursor to the item indicated by \var{key} and return a tuple
135containing the key and its value. For binary tree databases (opened
136using \function{btopen()}), if \var{key} does not actually exist in
137the database, the cursor will point to the next item in sorted order
138and return that key and value. For other databases,
139\exception{KeyError} will be raised if \var{key} is not found in the
140database.
Fred Drake9d158811999-04-19 21:19:21 +0000141\end{methoddesc}
142
143\begin{methoddesc}{first}{}
144Set the cursor to the first 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 Drakeba100c92004-08-10 19:22:48 +0000146This method raises \exception{bsddb.error} if the database is empty.
Fred Drake9d158811999-04-19 21:19:21 +0000147\end{methoddesc}
148
149\begin{methoddesc}{next}{}
150Set the cursor to the next item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000151keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000152\end{methoddesc}
153
154\begin{methoddesc}{previous}{}
Skip Montanaro61418122002-11-17 11:09:50 +0000155Set the cursor to the previous item in the DB file and return it. The
Fred Drake29cf6821999-04-23 20:32:59 +0000156order of keys in the file is unspecified, except in the case of B-Tree
157databases. This is not supported on hashtable databases (those opened
158with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000159\end{methoddesc}
160
161\begin{methoddesc}{last}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000162Set the cursor to the last item in the DB file and return it. The
163order of keys in the file is unspecified. This is not supported on
164hashtable databases (those opened with \function{hashopen()}).
Fred Drakeba100c92004-08-10 19:22:48 +0000165This method raises \exception{bsddb.error} if the database is empty.
Fred Drake9d158811999-04-19 21:19:21 +0000166\end{methoddesc}
167
168\begin{methoddesc}{sync}{}
169Synchronize the database on disk.
170\end{methoddesc}
171
172Example:
173
174\begin{verbatim}
175>>> import bsddb
176>>> db = bsddb.btopen('/tmp/spam.db', 'c')
177>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
178...
179>>> db['3']
180'9'
181>>> db.keys()
182['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
183>>> db.first()
184('0', '0')
185>>> db.next()
186('1', '1')
187>>> db.last()
188('9', '81')
189>>> db.set_location('2')
190('2', '4')
191>>> db.previous()
192('1', '1')
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000193>>> for k, v in db.iteritems():
194... print k, v
1950 0
1961 1
1972 4
1983 9
1994 16
2005 25
2016 36
2027 49
2038 64
2049 81
Raymond Hettingerff294fe2003-12-07 13:00:25 +0000205>>> '8' in db
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000206True
Fred Drake9d158811999-04-19 21:19:21 +0000207>>> db.sync()
2080
209\end{verbatim}