blob: bedfad4267212ae3b7d7a84e54ea7d4bcac8c9a8 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`dbhash` --- DBM-style interface to the BSD database library
2=================================================================
3
4.. module:: dbhash
5 :synopsis: DBM-style interface to the BSD database library.
6.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
7
Georg Brandl68d3eb92008-05-26 10:22:15 +00008.. note::
9 The :mod:`dbhash` module has been renamed to :mod:`dbm.bsd` in Python 3.0.
10 The :term:`2to3` tool will automatically adapt imports when converting your
11 sources to 3.0.
Georg Brandl8ec7f652007-08-15 14:28:01 +000012
13.. index:: module: bsddb
14
15The :mod:`dbhash` module provides a function to open databases using the BSD
16``db`` library. This module mirrors the interface of the other Python database
17modules that provide access to DBM-style databases. The :mod:`bsddb` module is
18required to use :mod:`dbhash`.
19
20This module provides an exception and a function:
21
22
23.. exception:: error
24
25 Exception raised on database errors other than :exc:`KeyError`. It is a synonym
26 for :exc:`bsddb.error`.
27
28
29.. function:: open(path[, flag[, mode]])
30
31 Open a ``db`` database and return the database object. The *path* argument is
32 the name of the database file.
33
34 The *flag* argument can be:
35
36 +---------+-------------------------------------------+
37 | Value | Meaning |
38 +=========+===========================================+
39 | ``'r'`` | Open existing database for reading only |
40 | | (default) |
41 +---------+-------------------------------------------+
42 | ``'w'`` | Open existing database for reading and |
43 | | writing |
44 +---------+-------------------------------------------+
45 | ``'c'`` | Open database for reading and writing, |
46 | | creating it if it doesn't exist |
47 +---------+-------------------------------------------+
48 | ``'n'`` | Always create a new, empty database, open |
49 | | for reading and writing |
50 +---------+-------------------------------------------+
51
52 For platforms on which the BSD ``db`` library supports locking, an ``'l'``
53 can be appended to indicate that locking should be used.
54
55 The optional *mode* parameter is used to indicate the Unix permission bits that
56 should be set if a new database must be created; this will be masked by the
57 current umask value for the process.
58
59
60.. seealso::
61
62 Module :mod:`anydbm`
63 Generic interface to ``dbm``\ -style databases.
64
65 Module :mod:`bsddb`
66 Lower-level interface to the BSD ``db`` library.
67
68 Module :mod:`whichdb`
69 Utility module used to determine the type of an existing database.
70
71
72.. _dbhash-objects:
73
74Database Objects
75----------------
76
77The database objects returned by :func:`open` provide the methods common to all
78the DBM-style databases and mapping objects. The following methods are
79available in addition to the standard methods.
80
81
82.. method:: dbhash.first()
83
84 It's possible to loop over every key/value pair in the database using this
85 method and the :meth:`next` method. The traversal is ordered by the databases
86 internal hash values, and won't be sorted by the key values. This method
87 returns the starting key.
88
89
90.. method:: dbhash.last()
91
92 Return the last key/value pair in a database traversal. This may be used to
93 begin a reverse-order traversal; see :meth:`previous`.
94
95
96.. method:: dbhash.next()
97
98 Returns the key next key/value pair in a database traversal. The following code
99 prints every key in the database ``db``, without having to create a list in
100 memory that contains them all::
101
102 print db.first()
103 for i in xrange(1, len(db)):
104 print db.next()
105
106
107.. method:: dbhash.previous()
108
109 Returns the previous key/value pair in a forward-traversal of the database. In
110 conjunction with :meth:`last`, this may be used to implement a reverse-order
111 traversal.
112
113
114.. method:: dbhash.sync()
115
116 This method forces any unwritten data to be written to the disk.
117