blob: cf4470721eabc5ac80af26e0f56d83b4c41e671b [file] [log] [blame]
Fred Drake666255f1999-04-15 15:11:40 +00001\section{\module{dbhash} ---
2 DBM-style interface to the BSD database library}
3
4\declaremodule{standard}{dbhash}
Fred Drake666255f1999-04-15 15:11:40 +00005\modulesynopsis{DBM-style interface to the BSD database library.}
Fred Drake31bfbc61999-04-16 14:03:32 +00006\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
Fred Drake666255f1999-04-15 15:11:40 +00007
8
9The \module{dbhash} module provides a function to open databases using
10the BSD \code{db} library. This module mirrors the interface of the
11other Python database modules that provide access to DBM-style
Fred Drake9d158811999-04-19 21:19:21 +000012databases. The \refmodule{bsddb}\refbimodindex{bsddb} module is required
Fred Drake666255f1999-04-15 15:11:40 +000013to use \module{dbhash}.
14
15This module provides an exception and a function:
16
17
18\begin{excdesc}{error}
19 Exception raised on database errors other than
20 \exception{KeyError}. It is a synonym for \exception{bsddb.error}.
21\end{excdesc}
22
Thomas Wouters69940972001-06-27 13:49:59 +000023\begin{funcdesc}{open}{path\optional{, flag\optional{, mode}}}
Fred Drake666255f1999-04-15 15:11:40 +000024 Open a \code{db} database and return the database object. The
25 \var{path} argument is the name of the database file.
26
27 The \var{flag} argument can be
Fred Drake31bfbc61999-04-16 14:03:32 +000028 \code{'r'} (the default), \code{'w'},
Fred Drake666255f1999-04-15 15:11:40 +000029 \code{'c'} (which creates the database if it doesn't exist), or
30 \code{'n'} (which always creates a new empty database).
31 For platforms on which the BSD \code{db} library supports locking,
32 an \character{l} can be appended to indicate that locking should be
33 used.
34
35 The optional \var{mode} parameter is used to indicate the \UNIX{}
36 permission bits that should be set if a new database must be
37 created; this will be masked by the current umask value for the
38 process.
39\end{funcdesc}
40
41
42\begin{seealso}
43 \seemodule{anydbm}{Generic interface to \code{dbm}-style databases.}
Fred Drake9d158811999-04-19 21:19:21 +000044 \seemodule{bsddb}{Lower-level interface to the BSD \code{db} library.}
Fred Drake666255f1999-04-15 15:11:40 +000045 \seemodule{whichdb}{Utility module used to determine the type of an
46 existing database.}
47\end{seealso}
48
49
50\subsection{Database Objects \label{dbhash-objects}}
51
52The database objects returned by \function{open()} provide the methods
Raymond Hettingerdeadbf52003-09-12 06:33:37 +000053common to all the DBM-style databases and mapping objects. The following
54methods are available in addition to the standard methods.
Fred Drake666255f1999-04-15 15:11:40 +000055
56\begin{methoddesc}[dbhash]{first}{}
Raymond Hettingerdeadbf52003-09-12 06:33:37 +000057 It's possible to loop over every key/value pair in the database using
58 this method and the \method{next()} method. The traversal is ordered by
Fred Drake666255f1999-04-15 15:11:40 +000059 the databases internal hash values, and won't be sorted by the key
60 values. This method returns the starting key.
61\end{methoddesc}
62
63\begin{methoddesc}[dbhash]{last}{}
Raymond Hettingerdeadbf52003-09-12 06:33:37 +000064 Return the last key/value pair in a database traversal. This may be used to
Fred Drake666255f1999-04-15 15:11:40 +000065 begin a reverse-order traversal; see \method{previous()}.
66\end{methoddesc}
67
Raymond Hettinger0d6e8cd2003-09-10 04:44:29 +000068\begin{methoddesc}[dbhash]{next}{}
Raymond Hettingerdeadbf52003-09-12 06:33:37 +000069 Returns the key next key/value pair in a database traversal. The
Fred Drake666255f1999-04-15 15:11:40 +000070 following code prints every key in the database \code{db}, without
71 having to create a list in memory that contains them all:
72
73\begin{verbatim}
Raymond Hettinger0d6e8cd2003-09-10 04:44:29 +000074print db.first()
Raymond Hettinger34040342003-09-16 21:45:22 +000075for i in xrange(1, len(db)):
Raymond Hettinger0d6e8cd2003-09-10 04:44:29 +000076 print db.next()
Fred Drake666255f1999-04-15 15:11:40 +000077\end{verbatim}
78\end{methoddesc}
79
Raymond Hettinger0d6e8cd2003-09-10 04:44:29 +000080\begin{methoddesc}[dbhash]{previous}{}
Raymond Hettingerdeadbf52003-09-12 06:33:37 +000081 Returns the previous key/value pair in a forward-traversal of the database.
Raymond Hettinger0d6e8cd2003-09-10 04:44:29 +000082 In conjunction with \method{last()}, this may be used to implement
83 a reverse-order traversal.
Fred Drake666255f1999-04-15 15:11:40 +000084\end{methoddesc}
85
86\begin{methoddesc}[dbhash]{sync}{}
87 This method forces any unwritten data to be written to the disk.
88\end{methoddesc}