blob: ea655a3e21ff3a4d2c21b5e7bed2c99aca6d760b [file] [log] [blame]
Guido van Rossumb69e0951994-08-08 08:03:24 +00001\section{Built-in Module \sectcode{gdbm}}
Guido van Rossum3c2a0561997-07-17 16:29:42 +00002\label{module-gdbm}
Guido van Rossumb69e0951994-08-08 08:03:24 +00003\bimodindex{gdbm}
4
Fred Drakec2297c11997-12-04 04:45:28 +00005This module is quite similar to the \code{dbm} module, but uses \code{gdbm}
Guido van Rossum3c2a0561997-07-17 16:29:42 +00006instead to provide some additional functionality. Please note that
Fred Drakec2297c11997-12-04 04:45:28 +00007the file formats created by \code{gdbm} and \code{dbm} are incompatible.
Fred Drake4f496cc1997-12-16 04:08:24 +00008\refbimodindex{dbm}
Guido van Rossum3c2a0561997-07-17 16:29:42 +00009
10The \code{gdbm} module provides an interface to the GNU DBM
Fred Drakec2297c11997-12-04 04:45:28 +000011library. \code{gdbm} objects behave like mappings
Guido van Rossum3c2a0561997-07-17 16:29:42 +000012(dictionaries), except that keys and values are always strings.
Fred Drakec2297c11997-12-04 04:45:28 +000013Printing a \code{gdbm} object doesn't print the keys and values, and the
Guido van Rossum3c2a0561997-07-17 16:29:42 +000014\code{items()} and \code{values()} methods are not supported.
15
16The module defines the following constant and functions:
17
Fred Drakec2297c11997-12-04 04:45:28 +000018\renewcommand{\indexsubitem}{(in module gdbm)}
Guido van Rossum3c2a0561997-07-17 16:29:42 +000019\begin{excdesc}{error}
Fred Drakec2297c11997-12-04 04:45:28 +000020Raised on \code{gdbm}-specific errors, such as I/O errors. \code{KeyError} is
Guido van Rossum3c2a0561997-07-17 16:29:42 +000021raised for general mapping errors like specifying an incorrect key.
22\end{excdesc}
23
24\begin{funcdesc}{open}{filename\, \optional{flag\, \optional{mode}}}
Fred Drakec2297c11997-12-04 04:45:28 +000025Open a \code{gdbm} database and return a \code{gdbm} object. The
26\var{filename} argument is the name of the database file.
Guido van Rossum3c2a0561997-07-17 16:29:42 +000027
28The optional \var{flag} argument can be
29\code{'r'} (to open an existing database for reading only --- default),
30\code{'w'} (to open an existing database for reading and writing),
31\code{'c'} (which creates the database if it doesn't exist), or
32\code{'n'} (which always creates a new empty database).
33
34Appending \code{f} to the flag opens the database in fast mode;
35altered data will not automatically be written to the disk after every
36change. This results in faster writes to the database, but may result
37in an inconsistent database if the program crashes while the database
38is still open. Use the \code{sync()} method to force any unwritten
39data to be written to the disk.
40
41The optional \var{mode} argument is the \UNIX{} mode of the file, used
42only when the database has to be created. It defaults to octal
43\code{0666}.
44\end{funcdesc}
45
Fred Drakec2297c11997-12-04 04:45:28 +000046In addition to the dictionary-like methods, \code{gdbm} objects have the
Guido van Rossum3c2a0561997-07-17 16:29:42 +000047following methods:
48
49\begin{funcdesc}{firstkey}{}
50It's possible to loop over every key in the database using this method
Fred Drakec2297c11997-12-04 04:45:28 +000051and the \code{nextkey()} method. The traversal is ordered by \code{gdbm}'s
Guido van Rossum3c2a0561997-07-17 16:29:42 +000052internal hash values, and won't be sorted by the key values. This
53method returns the starting key.
54\end{funcdesc}
55
56\begin{funcdesc}{nextkey}{key}
57Returns the key that follows \var{key} in the traversal. The
58following code prints every key in the database \code{db}, without having to
59create a list in memory that contains them all:
60\bcode\begin{verbatim}
61k=db.firstkey()
62while k!=None:
63 print k
64 k=db.nextkey(k)
65\end{verbatim}\ecode
66\end{funcdesc}
67
68\begin{funcdesc}{reorganize}{}
69If you have carried out a lot of deletions and would like to shrink
Fred Drakec2297c11997-12-04 04:45:28 +000070the space used by the \code{gdbm} file, this routine will reorganize the
71database. \code{gdbm} will not shorten the length of a database file except
Guido van Rossum3c2a0561997-07-17 16:29:42 +000072by using this reorganization; otherwise, deleted file space will be
73kept and reused as new (key,value) pairs are added.
74\end{funcdesc}
75
76\begin{funcdesc}{sync}{}
77When the database has been opened in fast mode, this method forces any
78unwritten data to be written to the disk.
79\end{funcdesc}
80