blob: 44b916868905b6c519a900845fabcce19452c216 [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 Wouters0e3f5912006-08-11 14:57:12 +000022 \seeurl{http://pybsddb.sourceforge.net/}{The website with documentation
23 for the \module{bsddb.db} python Berkeley DB interface that closely mirrors
24 the Sleepycat object oriented interface provided in Berkeley DB 3 and 4.}
Gregory P. Smith57725132003-05-28 07:56:45 +000025 \seeurl{http://www.sleepycat.com/}{Sleepycat Software produces the
Thomas Wouters0e3f5912006-08-11 14:57:12 +000026 Berkeley DB library.}
Gregory P. Smith57725132003-05-28 07:56:45 +000027\end{seealso}
28
Thomas Wouters0e3f5912006-08-11 14:57:12 +000029A more modern DB, DBEnv and DBSequence object interface is available in the
30\module{bsddb.db} module which closely matches the Sleepycat Berkeley DB C API
31documented at the above URLs. Additional features provided by the
32\module{bsddb.db} API include fine tuning, transactions, logging, and
33multiprocess concurrent database access.
34
Gregory P. Smith57725132003-05-28 07:56:45 +000035The following is a description of the legacy \module{bsddb} interface
Thomas Wouters0e3f5912006-08-11 14:57:12 +000036compatible with the old python bsddb module. Starting in Python 2.5 this
37interface should be safe for multithreaded access. The \module{bsddb.db}
38API is recommended for threading users as it provides better control.
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
Fred Drakefdccf1a2004-07-26 16:33:29 +000055\character{r} (read only), \character{w} (read-write) ,
56\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000057\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{,
Andrew M. Kuchling8dbe1a72005-06-08 21:51:28 +000065minkeypage\optional{, pgsize\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
Fred Drakefdccf1a2004-07-26 16:33:29 +000071\character{r} (read only), \character{w} (read-write),
72\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000073\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{,
Andrew M. Kuchling8dbe1a72005-06-08 21:51:28 +000080rnflags\optional{, cachesize\optional{, pgsize\optional{, lorder\optional{,
Fred Drake9d158811999-04-19 21:19:21 +000081reclen\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
Fred Drakefdccf1a2004-07-26 16:33:29 +000087\character{r} (read only), \character{w} (read-write),
88\character{c} (read-write - create if necessary; the default) or
Fred Drake38e5d272000-04-03 20:13:55 +000089\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
Skip Montanaro6d9f45b2003-05-06 20:40:17 +000096\begin{notice}
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097Beginning in 2.3 some \UNIX{} versions of Python may have a \module{bsddb185}
Skip Montanaro6d9f45b2003-05-06 20:40:17 +000098module. This is present \emph{only} to allow backwards compatibility with
99systems which ship with the old Berkeley DB 1.85 database library. The
100\module{bsddb185} module should never be used directly in new code.
101\end{notice}
Fred Drake9d158811999-04-19 21:19:21 +0000102
Fred Drakeb86aa992004-06-24 06:03:59 +0000103
104\begin{seealso}
105 \seemodule{dbhash}{DBM-style interface to the \module{bsddb}}
106\end{seealso}
107
Fred Drake9d158811999-04-19 21:19:21 +0000108\subsection{Hash, BTree and Record Objects \label{bsddb-objects}}
109
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000110Once instantiated, hash, btree and record objects support
111the same methods as dictionaries. In addition, they support
Raymond Hettinger34040342003-09-16 21:45:22 +0000112the methods listed below.
113\versionchanged[Added dictionary methods]{2.3.1}
Fred Drake9d158811999-04-19 21:19:21 +0000114
115\begin{methoddesc}{close}{}
116Close the underlying file. The object can no longer be accessed. Since
117there is no open \method{open} method for these objects, to open the file
118again a new \module{bsddb} module open function must be called.
119\end{methoddesc}
120
121\begin{methoddesc}{keys}{}
122Return the list of keys contained in the DB file. The order of the list is
123unspecified and should not be relied on. In particular, the order of the
124list returned is different for different file formats.
125\end{methoddesc}
126
127\begin{methoddesc}{has_key}{key}
Fred Drake38e5d272000-04-03 20:13:55 +0000128Return \code{1} if the DB file contains the argument as a key.
Fred Drake9d158811999-04-19 21:19:21 +0000129\end{methoddesc}
130
131\begin{methoddesc}{set_location}{key}
Fred Drakee1d47152001-01-05 06:44:19 +0000132Set the cursor to the item indicated by \var{key} and return a tuple
133containing the key and its value. For binary tree databases (opened
134using \function{btopen()}), if \var{key} does not actually exist in
135the database, the cursor will point to the next item in sorted order
136and return that key and value. For other databases,
137\exception{KeyError} will be raised if \var{key} is not found in the
138database.
Fred Drake9d158811999-04-19 21:19:21 +0000139\end{methoddesc}
140
141\begin{methoddesc}{first}{}
142Set the cursor to the first item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000143keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drakeba100c92004-08-10 19:22:48 +0000144This method raises \exception{bsddb.error} if the database is empty.
Fred Drake9d158811999-04-19 21:19:21 +0000145\end{methoddesc}
146
147\begin{methoddesc}{next}{}
148Set the cursor to the next item in the DB file and return it. The order of
Fred Drake29cf6821999-04-23 20:32:59 +0000149keys in the file is unspecified, except in the case of B-Tree databases.
Fred Drake9d158811999-04-19 21:19:21 +0000150\end{methoddesc}
151
152\begin{methoddesc}{previous}{}
Skip Montanaro61418122002-11-17 11:09:50 +0000153Set the cursor to the previous item in the DB file and return it. The
Fred Drake29cf6821999-04-23 20:32:59 +0000154order of keys in the file is unspecified, except in the case of B-Tree
155databases. This is not supported on hashtable databases (those opened
156with \function{hashopen()}).
Fred Drake9d158811999-04-19 21:19:21 +0000157\end{methoddesc}
158
159\begin{methoddesc}{last}{}
Fred Drake2ea30f41999-04-22 14:06:36 +0000160Set the cursor to the last item in the DB file and return it. The
161order of keys in the file is unspecified. This is not supported on
162hashtable databases (those opened with \function{hashopen()}).
Fred Drakeba100c92004-08-10 19:22:48 +0000163This method raises \exception{bsddb.error} if the database is empty.
Fred Drake9d158811999-04-19 21:19:21 +0000164\end{methoddesc}
165
166\begin{methoddesc}{sync}{}
167Synchronize the database on disk.
168\end{methoddesc}
169
170Example:
171
172\begin{verbatim}
173>>> import bsddb
174>>> db = bsddb.btopen('/tmp/spam.db', 'c')
175>>> for i in range(10): db['%d'%i] = '%d'% (i*i)
176...
177>>> db['3']
178'9'
179>>> db.keys()
180['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
181>>> db.first()
182('0', '0')
183>>> db.next()
184('1', '1')
185>>> db.last()
186('9', '81')
187>>> db.set_location('2')
188('2', '4')
189>>> db.previous()
190('1', '1')
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000191>>> for k, v in db.iteritems():
192... print k, v
1930 0
1941 1
1952 4
1963 9
1974 16
1985 25
1996 36
2007 49
2018 64
2029 81
Raymond Hettingerff294fe2003-12-07 13:00:25 +0000203>>> '8' in db
Raymond Hettingerdeadbf52003-09-12 06:33:37 +0000204True
Fred Drake9d158811999-04-19 21:19:21 +0000205>>> db.sync()
2060
207\end{verbatim}