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