blob: 6e86d24ef932d1e73f9e3f4c5f149cc3a9fcd47c [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}
Fred Drake9d158811999-04-19 21:19:21 +00005\modulesynopsis{Interface to Berkeley DB database library}
6\sectionauthor{Skip Montanaro}{skip@mojam.com}
7
8
Fred Drake38e5d272000-04-03 20:13:55 +00009The \module{bsddb} module provides an interface to the Berkeley DB
10library. Users can create hash, btree or record based library files
11using the appropriate open call. Bsddb objects behave generally like
12dictionaries. Keys and values must be strings, however, so to use
13other objects as keys or to store other kinds of objects the user must
Andrew M. Kuchling97473012005-12-22 20:12:54 +000014serialize them somehow, typically using \function{marshal.dumps()} or
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015\function{pickle.dumps()}.
Fred Drake9d158811999-04-19 21:19:21 +000016
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000017The \module{bsddb} module requires a Berkeley DB library version from
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000183.3 thru 4.5.
Gregory P. Smith57725132003-05-28 07:56:45 +000019
Gregory P. Smith57725132003-05-28 07:56:45 +000020\begin{seealso}
Thomas Wouters89f507f2006-12-13 04:49:30 +000021 \seeurl{http://pybsddb.sourceforge.net/}
22 {The website with documentation for the \module{bsddb.db}
23 Python Berkeley DB interface that closely mirrors the object
24 oriented interface provided in Berkeley DB 3 and 4.}
25
26 \seeurl{http://www.oracle.com/database/berkeley-db/}
27 {The Berkeley DB library.}
Gregory P. Smith57725132003-05-28 07:56:45 +000028\end{seealso}
29
Thomas Wouters0e3f5912006-08-11 14:57:12 +000030A more modern DB, DBEnv and DBSequence object interface is available in the
Thomas Wouters89f507f2006-12-13 04:49:30 +000031\module{bsddb.db} module which closely matches the Berkeley DB C API
Thomas Wouters0e3f5912006-08-11 14:57:12 +000032documented at the above URLs. Additional features provided by the
33\module{bsddb.db} API include fine tuning, transactions, logging, and
34multiprocess concurrent database access.
35
Gregory P. Smith57725132003-05-28 07:56:45 +000036The following is a description of the legacy \module{bsddb} interface
Thomas Wouters89f507f2006-12-13 04:49:30 +000037compatible with the old Python bsddb module. Starting in Python 2.5 this
Thomas Wouters0e3f5912006-08-11 14:57:12 +000038interface should be safe for multithreaded access. The \module{bsddb.db}
39API is recommended for threading users as it provides better control.
Fred Drake9d158811999-04-19 21:19:21 +000040
41The \module{bsddb} module defines the following functions that create
Fred Drake38e5d272000-04-03 20:13:55 +000042objects that access the appropriate type of Berkeley DB file. The
43first two arguments of each function are the same. For ease of
44portability, only the first two arguments should be used in most
45instances.
Fred Drake9d158811999-04-19 21:19:21 +000046
47\begin{funcdesc}{hashopen}{filename\optional{, flag\optional{,
Guido van Rossumd8faa362007-04-27 19:54:29 +000048 mode\optional{, pgsize\optional{,
Fred Drake38e5d272000-04-03 20:13:55 +000049 ffactor\optional{, nelem\optional{,
Guido van Rossumd8faa362007-04-27 19:54:29 +000050 cachesize\optional{, lorder\optional{,
51 hflags}}}}}}}}}
Anthony Baxter83888952002-04-23 02:11:05 +000052Open the hash format file named \var{filename}. Files never intended
53to be preserved on disk may be created by passing \code{None} as the
54\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000055\var{flag} identifies the mode used to open the file. It may be
Fred Drakefdccf1a2004-07-26 16:33:29 +000056\character{r} (read only), \character{w} (read-write) ,
57\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000058\character{n} (read-write - truncate to zero length). The other
59arguments are rarely used and are just passed to the low-level
60\cfunction{dbopen()} function. Consult the Berkeley DB documentation
61for their use and interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000062\end{funcdesc}
63
Fred Drake9d158811999-04-19 21:19:21 +000064\begin{funcdesc}{btopen}{filename\optional{, flag\optional{,
65mode\optional{, btflags\optional{, cachesize\optional{, maxkeypage\optional{,
Andrew M. Kuchling8dbe1a72005-06-08 21:51:28 +000066minkeypage\optional{, pgsize\optional{, lorder}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000067
Anthony Baxter83888952002-04-23 02:11:05 +000068Open the btree format file named \var{filename}. Files never intended
69to be preserved on disk may be created by passing \code{None} as the
70\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000071\var{flag} identifies the mode used to open the file. It may be
Fred Drakefdccf1a2004-07-26 16:33:29 +000072\character{r} (read only), \character{w} (read-write),
73\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000074\character{n} (read-write - truncate to zero length). The other
75arguments are rarely used and are just passed to the low-level dbopen
76function. Consult the Berkeley DB documentation for their use and
77interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000078\end{funcdesc}
79
80\begin{funcdesc}{rnopen}{filename\optional{, flag\optional{, mode\optional{,
Andrew M. Kuchling8dbe1a72005-06-08 21:51:28 +000081rnflags\optional{, cachesize\optional{, pgsize\optional{, lorder\optional{,
Guido van Rossumd8faa362007-04-27 19:54:29 +000082rlen\optional{, delim\optional{, source\optional{, pad}}}}}}}}}}}
Fred Drake38e5d272000-04-03 20:13:55 +000083
Anthony Baxter83888952002-04-23 02:11:05 +000084Open a DB record format file named \var{filename}. Files never intended
85to be preserved on disk may be created by passing \code{None} as the
86\var{filename}. The optional
Fred Drake38e5d272000-04-03 20:13:55 +000087\var{flag} identifies the mode used to open the file. It may be
Fred Drakefdccf1a2004-07-26 16:33:29 +000088\character{r} (read only), \character{w} (read-write),
89\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000090\character{n} (read-write - truncate to zero length). The other
91arguments are rarely used and are just passed to the low-level dbopen
92function. Consult the Berkeley DB documentation for their use and
93interpretation.
Fred Drake9d158811999-04-19 21:19:21 +000094\end{funcdesc}
95
96
Skip Montanaro6d9f45b2003-05-06 20:40:17 +000097\begin{notice}
Thomas Wouters0e3f5912006-08-11 14:57:12 +000098Beginning in 2.3 some \UNIX{} versions of Python may have a \module{bsddb185}
Skip Montanaro6d9f45b2003-05-06 20:40:17 +000099module. This is present \emph{only} to allow backwards compatibility with
100systems which ship with the old Berkeley DB 1.85 database library. The
101\module{bsddb185} module should never be used directly in new code.
102\end{notice}
Fred Drake9d158811999-04-19 21:19:21 +0000103
Fred Drakeb86aa992004-06-24 06:03:59 +0000104
105\begin{seealso}
106 \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
107\end{seealso}
108
Fred Drake9d158811999-04-19 21:19:21 +0000109\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
110
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000111Once instantiated, hash, btree and record objects support
112the same methods as dictionaries. In addition, they support
Raymond Hettinger34040342003-09-16 21:45:22 +0000113the methods listed below.
114\versionchanged[Added dictionary methods]{2.3.1}
Fred Drake9d158811999-04-19 21:19:21 +0000115
Guido van Rossumd8faa362007-04-27 19:54:29 +0000116\begin{methoddesc}[bsddbobject]{close}{}
Fred Drake9d158811999-04-19 21:19:21 +0000117Close the underlying file. The object can no longer be accessed. Since
118there is no open \method{open} method for these objects, to open the file
119again a new \module{bsddb} module open function must be called.
120\end{methoddesc}
121
Guido van Rossumd8faa362007-04-27 19:54:29 +0000122\begin{methoddesc}[bsddbobject]{keys}{}
Fred Drake9d158811999-04-19 21:19:21 +0000123Return the list of keys contained in the DB file. The order of the list is
124unspecified and should not be relied on. In particular, the order of the
125list returned is different for different file formats.
126\end{methoddesc}
127
Guido van Rossumd8faa362007-04-27 19:54:29 +0000128\begin{methoddesc}[bsddbobject]{has_key}{key}
Fred Drake38e5d272000-04-03 20:13:55 +0000129Return \code{1} if the DB file contains the argument as a key.
Fred Drake9d158811999-04-19 21:19:21 +0000130\end{methoddesc}
131
Guido van Rossumd8faa362007-04-27 19:54:29 +0000132\begin{methoddesc}[bsddbobject]{set_location}{key}
Fred Drakee1d47152001-01-05 06:44:19 +0000133Set the cursor to the item indicated by \var{key} and return a tuple
134containing the key and its value. For binary tree databases (opened
135using \function{btopen()}), if \var{key} does not actually exist in
136the database, the cursor will point to the next item in sorted order
137and return that key and value. For other databases,
138\exception{KeyError} will be raised if \var{key} is not found in the
139database.
Fred Drake9d158811999-04-19 21:19:21 +0000140\end{methoddesc}
141
Guido van Rossumd8faa362007-04-27 19:54:29 +0000142\begin{methoddesc}[bsddbobject]{first}{}
Fred Drake9d158811999-04-19 21:19:21 +0000143Set the cursor to the first 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 Drakeba100c92004-08-10 19:22:48 +0000145This method raises \exception{bsddb.error} if the database is empty.
Fred Drake9d158811999-04-19 21:19:21 +0000146\end{methoddesc}
147
Guido van Rossumd8faa362007-04-27 19:54:29 +0000148\begin{methoddesc}[bsddbobject]{next}{}
Fred Drake9d158811999-04-19 21:19:21 +0000149Set the cursor to the next item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000150keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000151\end{methoddesc}
152
Guido van Rossumd8faa362007-04-27 19:54:29 +0000153\begin{methoddesc}[bsddbobject]{previous}{}
Skip Montanaro61418122002-11-17 11:09:50 +0000154Set the cursor to the previous item in the DB file and return it. The
Fred Drake29cf6821999-04-23 20:32:59 +0000155order of keys in the file is unspecified, except in the case of B-Tree
156databases. This is not supported on hashtable databases (those opened
157with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000158\end{methoddesc}
159
Guido van Rossumd8faa362007-04-27 19:54:29 +0000160\begin{methoddesc}[bsddbobject]{last}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000161Set the cursor to the last item in the DB file and return it. The
162order of keys in the file is unspecified. This is not supported on
163hashtable databases (those opened with \function{hashopen()}).
Fred Drakeba100c92004-08-10 19:22:48 +0000164This method raises \exception{bsddb.error} if the database is empty.
Fred Drake9d158811999-04-19 21:19:21 +0000165\end{methoddesc}
166
Guido van Rossumd8faa362007-04-27 19:54:29 +0000167\begin{methoddesc}[bsddbobject]{sync}{}
Fred Drake9d158811999-04-19 21:19:21 +0000168Synchronize the database on disk.
169\end{methoddesc}
170
171Example:
172
173\begin{verbatim}
174>>> import bsddb
175>>> db = bsddb.btopen('/tmp/spam.db', 'c')
176>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
177...
178>>> db['3']
179'9'
180>>> db.keys()
181['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
182>>> db.first()
183('0', '0')
184>>> db.next()
185('1', '1')
186>>> db.last()
187('9', '81')
188>>> db.set_location('2')
189('2', '4')
190>>> db.previous()
191('1', '1')
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000192>>> for k, v in db.iteritems():
193... print k, v
1940 0
1951 1
1962 4
1973 9
1984 16
1995 25
2006 36
2017 49
2028 64
2039 81
Raymond Hettingerff294fe2003-12-07 13:00:25 +0000204>>> '8' in db
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000205True
Fred Drake9d158811999-04-19 21:19:21 +0000206>>> db.sync()
2070
208\end{verbatim}