blob: 52923e826de1927b4144203b6fb90c7b3989b765 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`dbm` --- Simple "database" interface
3==========================================
4
5.. module:: dbm
6 :platform: Unix
7 :synopsis: The standard "database" interface, based on ndbm.
8
9
10The :mod:`dbm` module provides an interface to the Unix "(n)dbm" library. Dbm
11objects behave like mappings (dictionaries), except that keys and values are
12always strings. Printing a dbm object doesn't print the keys and values, and the
13:meth:`items` and :meth:`values` methods are not supported.
14
15This module can be used with the "classic" ndbm interface, the BSD DB
16compatibility interface, or the GNU GDBM compatibility interface. On Unix, the
17:program:`configure` script will attempt to locate the appropriate header file
18to simplify building this module.
19
20The module defines the following:
21
22
23.. exception:: error
24
25 Raised on dbm-specific errors, such as I/O errors. :exc:`KeyError` is raised for
26 general mapping errors like specifying an incorrect key.
27
28
29.. data:: library
30
31 Name of the ``ndbm`` implementation library used.
32
33
34.. function:: open(filename[, flag[, mode]])
35
36 Open a dbm database and return a dbm object. The *filename* argument is the
37 name of the database file (without the :file:`.dir` or :file:`.pag` extensions;
38 note that the BSD DB implementation of the interface will append the extension
39 :file:`.db` and only create one file).
40
41 The optional *flag* argument must be one of these values:
42
43 +---------+-------------------------------------------+
44 | Value | Meaning |
45 +=========+===========================================+
46 | ``'r'`` | Open existing database for reading only |
47 | | (default) |
48 +---------+-------------------------------------------+
49 | ``'w'`` | Open existing database for reading and |
50 | | writing |
51 +---------+-------------------------------------------+
52 | ``'c'`` | Open database for reading and writing, |
53 | | creating it if it doesn't exist |
54 +---------+-------------------------------------------+
55 | ``'n'`` | Always create a new, empty database, open |
56 | | for reading and writing |
57 +---------+-------------------------------------------+
58
59 The optional *mode* argument is the Unix mode of the file, used only when the
60 database has to be created. It defaults to octal ``0666`` (and will be
61 modified by the prevailing umask).
62
63
64.. seealso::
65
66 Module :mod:`anydbm`
67 Generic interface to ``dbm``\ -style databases.
68
69 Module :mod:`gdbm`
70 Similar interface to the GNU GDBM library.
71
72 Module :mod:`whichdb`
73 Utility module used to determine the type of an existing database.
74