blob: badc6ecfc23e7fd404e1563b63150536b905f926 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{anydbm} ---
Fred Drakebbac4321999-02-20 00:14:17 +00002 Generic access to DBM-style databases}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakebbac4321999-02-20 00:14:17 +00004\declaremodule{standard}{anydbm}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Generic interface to DBM-style database modules.}
6
Guido van Rossum571391b1997-04-03 22:41:49 +00007
Fred Drakeddf03bf1998-02-18 15:05:47 +00008\module{anydbm} is a generic interface to variants of the DBM
Fred Drake666255f1999-04-15 15:11:40 +00009database --- \refmodule{dbhash}\refstmodindex{dbhash} (requires
Fred Drake9d158811999-04-19 21:19:21 +000010\refmodule{bsddb}\refbimodindex{bsddb}),
Fred Drake01553701999-04-05 19:46:21 +000011\refmodule{gdbm}\refbimodindex{gdbm}, or
12\refmodule{dbm}\refbimodindex{dbm}. If none of these modules is
13installed, the slow-but-simple implementation in module
14\refmodule{dumbdbm}\refstmodindex{dumbdbm} will be used.
Guido van Rossum571391b1997-04-03 22:41:49 +000015
Fred Drake2764dd31998-04-07 22:08:02 +000016\begin{funcdesc}{open}{filename\optional{, flag\optional{, mode}}}
Fred Drakeddf03bf1998-02-18 15:05:47 +000017Open the database file \var{filename} and return a corresponding object.
Guido van Rossum2aefe8d1998-04-28 15:29:26 +000018
Fred Drake01553701999-04-05 19:46:21 +000019If the database file already exists, the \refmodule{whichdb} module is
Guido van Rossum2aefe8d1998-04-28 15:29:26 +000020used to determine its type and the appropriate module is used; if it
Fred Drake01553701999-04-05 19:46:21 +000021does not exist, the first module listed above that can be imported is
Guido van Rossum2aefe8d1998-04-28 15:29:26 +000022used.
23
Guido van Rossum571391b1997-04-03 22:41:49 +000024The optional \var{flag} argument can be
25\code{'r'} to open an existing database for reading only,
26\code{'w'} to open an existing database for reading and writing,
27\code{'c'} to create the database if it doesn't exist, or
28\code{'n'}, which will always create a new empty database. If not
29specified, the default value is \code{'r'}.
30
31The optional \var{mode} argument is the \UNIX{} mode of the file, used
32only when the database has to be created. It defaults to octal
Guido van Rossum51a6c901997-05-09 02:23:45 +000033\code{0666} (and will be modified by the prevailing umask).
Guido van Rossum571391b1997-04-03 22:41:49 +000034\end{funcdesc}
35
Fred Drake2764dd31998-04-07 22:08:02 +000036\begin{excdesc}{error}
Guido van Rossum2aefe8d1998-04-28 15:29:26 +000037A tuple containing the exceptions that can be raised by each of the
38supported modules, with a unique exception \exception{anydbm.error} as
39the first item --- the latter is used when \exception{anydbm.error} is
40raised.
Fred Drake2764dd31998-04-07 22:08:02 +000041\end{excdesc}
42
Fred Drakeddf03bf1998-02-18 15:05:47 +000043The object returned by \function{open()} supports most of the same
Guido van Rossum571391b1997-04-03 22:41:49 +000044functionality as dictionaries; keys and their corresponding values can
Fred Drakeddf03bf1998-02-18 15:05:47 +000045be stored, retrieved, and deleted, and the \method{has_key()} and
46\method{keys()} methods are available. Keys and values must always be
Guido van Rossum51a6c901997-05-09 02:23:45 +000047strings.
Guido van Rossum571391b1997-04-03 22:41:49 +000048
Andrew M. Kuchling39d77392006-07-28 12:48:07 +000049The following example records some hostnames and a corresponding title,
50and then prints out the contents of the database:
51
52\begin{verbatim}
53import anydbm
54
55# Open database, creating it if necessary.
56db = anydbm.open('cache', 'c')
57
58# Record some values
59db['www.python.org'] = 'Python Website'
60db['www.cnn.com'] = 'Cable News Network'
61
62# Loop through contents. Other dictionary methods
63# such as .keys(), .values() also work.
64for k, v in db.iteritems():
65 print k, '\t', v
66
67# Storing a non-string key or value will raise an exception (most
68# likely a TypeError).
69db['www.yahoo.com'] = 4
70
71# Close when done.
72db.close()
73\end{verbatim}
74
Fred Drake2764dd31998-04-07 22:08:02 +000075
Fred Drake01553701999-04-05 19:46:21 +000076\begin{seealso}
Fred Drake666255f1999-04-15 15:11:40 +000077 \seemodule{dbhash}{BSD \code{db} database interface.}
Fred Drake01553701999-04-05 19:46:21 +000078 \seemodule{dbm}{Standard \UNIX{} database interface.}
79 \seemodule{dumbdbm}{Portable implementation of the \code{dbm} interface.}
80 \seemodule{gdbm}{GNU database interface, based on the \code{dbm} interface.}
81 \seemodule{shelve}{General object persistence built on top of
82 the Python \code{dbm} interface.}
83 \seemodule{whichdb}{Utility module used to determine the type of an
84 existing database.}
85\end{seealso}