Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="module-bsddb.html" /> | |
13 | <link rel="parent" href="module-bsddb.html" /> | |
14 | <link rel="next" href="module-dumbdbm.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>7.13.1 Hash, BTree and Record Objects </title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="7.13 bsddb " | |
24 | href="module-bsddb.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="7.13 bsddb " | |
27 | href="module-bsddb.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="7.14 dumbdbm " | |
30 | href="module-dumbdbm.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="module-bsddb.html">7.13 bsddb </A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="module-bsddb.html">7.13 bsddb </A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="module-dumbdbm.html">7.14 dumbdbm </A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION0091310000000000000000"></A><A NAME="bsddb-objects"></A> | |
55 | <BR> | |
56 | 7.13.1 Hash, BTree and Record Objects | |
57 | </H2> | |
58 | ||
59 | <P> | |
60 | Once instantiated, hash, btree and record objects support | |
61 | the same methods as dictionaries. In addition, they support | |
62 | the methods listed below. | |
63 | ||
64 | <span class="versionnote">Changed in version 2.3.1: | |
65 | Added dictionary methods.</span> | |
66 | ||
67 | <P> | |
68 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
69 | <td><nobr><b><tt id='l2h-2800' xml:id='l2h-2800' class="method">close</tt></b>(</nobr></td> | |
70 | <td><var></var>)</td></tr></table></dt> | |
71 | <dd> | |
72 | Close the underlying file. The object can no longer be accessed. Since | |
73 | there is no open <tt class="method">open</tt> method for these objects, to open the file | |
74 | again a new <tt class="module">bsddb</tt> module open function must be called. | |
75 | </dl> | |
76 | ||
77 | <P> | |
78 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
79 | <td><nobr><b><tt id='l2h-2801' xml:id='l2h-2801' class="method">keys</tt></b>(</nobr></td> | |
80 | <td><var></var>)</td></tr></table></dt> | |
81 | <dd> | |
82 | Return the list of keys contained in the DB file. The order of the list is | |
83 | unspecified and should not be relied on. In particular, the order of the | |
84 | list returned is different for different file formats. | |
85 | </dl> | |
86 | ||
87 | <P> | |
88 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
89 | <td><nobr><b><tt id='l2h-2802' xml:id='l2h-2802' class="method">has_key</tt></b>(</nobr></td> | |
90 | <td><var>key</var>)</td></tr></table></dt> | |
91 | <dd> | |
92 | Return <code>1</code> if the DB file contains the argument as a key. | |
93 | </dl> | |
94 | ||
95 | <P> | |
96 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
97 | <td><nobr><b><tt id='l2h-2803' xml:id='l2h-2803' class="method">set_location</tt></b>(</nobr></td> | |
98 | <td><var>key</var>)</td></tr></table></dt> | |
99 | <dd> | |
100 | Set the cursor to the item indicated by <var>key</var> and return a tuple | |
101 | containing the key and its value. For binary tree databases (opened | |
102 | using <tt class="function">btopen()</tt>), if <var>key</var> does not actually exist in | |
103 | the database, the cursor will point to the next item in sorted order | |
104 | and return that key and value. For other databases, | |
105 | <tt class="exception">KeyError</tt> will be raised if <var>key</var> is not found in the | |
106 | database. | |
107 | </dl> | |
108 | ||
109 | <P> | |
110 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
111 | <td><nobr><b><tt id='l2h-2804' xml:id='l2h-2804' class="method">first</tt></b>(</nobr></td> | |
112 | <td><var></var>)</td></tr></table></dt> | |
113 | <dd> | |
114 | Set the cursor to the first item in the DB file and return it. The order of | |
115 | keys in the file is unspecified, except in the case of B-Tree databases. | |
116 | This method raises <tt class="exception">bsddb.error</tt> if the database is empty. | |
117 | </dl> | |
118 | ||
119 | <P> | |
120 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
121 | <td><nobr><b><tt id='l2h-2805' xml:id='l2h-2805' class="method">next</tt></b>(</nobr></td> | |
122 | <td><var></var>)</td></tr></table></dt> | |
123 | <dd> | |
124 | Set the cursor to the next item in the DB file and return it. The order of | |
125 | keys in the file is unspecified, except in the case of B-Tree databases. | |
126 | </dl> | |
127 | ||
128 | <P> | |
129 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
130 | <td><nobr><b><tt id='l2h-2806' xml:id='l2h-2806' class="method">previous</tt></b>(</nobr></td> | |
131 | <td><var></var>)</td></tr></table></dt> | |
132 | <dd> | |
133 | Set the cursor to the previous item in the DB file and return it. The | |
134 | order of keys in the file is unspecified, except in the case of B-Tree | |
135 | databases. This is not supported on hashtable databases (those opened | |
136 | with <tt class="function">hashopen()</tt>). | |
137 | </dl> | |
138 | ||
139 | <P> | |
140 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
141 | <td><nobr><b><tt id='l2h-2807' xml:id='l2h-2807' class="method">last</tt></b>(</nobr></td> | |
142 | <td><var></var>)</td></tr></table></dt> | |
143 | <dd> | |
144 | Set the cursor to the last item in the DB file and return it. The | |
145 | order of keys in the file is unspecified. This is not supported on | |
146 | hashtable databases (those opened with <tt class="function">hashopen()</tt>). | |
147 | This method raises <tt class="exception">bsddb.error</tt> if the database is empty. | |
148 | </dl> | |
149 | ||
150 | <P> | |
151 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
152 | <td><nobr><b><tt id='l2h-2808' xml:id='l2h-2808' class="method">sync</tt></b>(</nobr></td> | |
153 | <td><var></var>)</td></tr></table></dt> | |
154 | <dd> | |
155 | Synchronize the database on disk. | |
156 | </dl> | |
157 | ||
158 | <P> | |
159 | Example: | |
160 | ||
161 | <P> | |
162 | <div class="verbatim"><pre> | |
163 | >>> import bsddb | |
164 | >>> db = bsddb.btopen('/tmp/spam.db', 'c') | |
165 | >>> for i in range(10): db['%d'%i] = '%d'% (i*i) | |
166 | ... | |
167 | >>> db['3'] | |
168 | '9' | |
169 | >>> db.keys() | |
170 | ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] | |
171 | >>> db.first() | |
172 | ('0', '0') | |
173 | >>> db.next() | |
174 | ('1', '1') | |
175 | >>> db.last() | |
176 | ('9', '81') | |
177 | >>> db.set_location('2') | |
178 | ('2', '4') | |
179 | >>> db.previous() | |
180 | ('1', '1') | |
181 | >>> for k, v in db.iteritems(): | |
182 | ... print k, v | |
183 | 0 0 | |
184 | 1 1 | |
185 | 2 4 | |
186 | 3 9 | |
187 | 4 16 | |
188 | 5 25 | |
189 | 6 36 | |
190 | 7 49 | |
191 | 8 64 | |
192 | 9 81 | |
193 | >>> '8' in db | |
194 | True | |
195 | >>> db.sync() | |
196 | 0 | |
197 | </pre></div> | |
198 | ||
199 | <DIV CLASS="navigation"> | |
200 | <div class='online-navigation'> | |
201 | <p></p><hr /> | |
202 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
203 | <tr> | |
204 | <td class='online-navigation'><a rel="prev" title="7.13 bsddb " | |
205 | href="module-bsddb.html"><img src='../icons/previous.png' | |
206 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
207 | <td class='online-navigation'><a rel="parent" title="7.13 bsddb " | |
208 | href="module-bsddb.html"><img src='../icons/up.png' | |
209 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
210 | <td class='online-navigation'><a rel="next" title="7.14 dumbdbm " | |
211 | href="module-dumbdbm.html"><img src='../icons/next.png' | |
212 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
213 | <td align="center" width="100%">Python Library Reference</td> | |
214 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
215 | href="contents.html"><img src='../icons/contents.png' | |
216 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
217 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
218 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
219 | <td class='online-navigation'><a rel="index" title="Index" | |
220 | href="genindex.html"><img src='../icons/index.png' | |
221 | border='0' height='32' alt='Index' width='32' /></A></td> | |
222 | </tr></table> | |
223 | <div class='online-navigation'> | |
224 | <b class="navlabel">Previous:</b> | |
225 | <a class="sectref" rel="prev" href="module-bsddb.html">7.13 bsddb </A> | |
226 | <b class="navlabel">Up:</b> | |
227 | <a class="sectref" rel="parent" href="module-bsddb.html">7.13 bsddb </A> | |
228 | <b class="navlabel">Next:</b> | |
229 | <a class="sectref" rel="next" href="module-dumbdbm.html">7.14 dumbdbm </A> | |
230 | </div> | |
231 | </div> | |
232 | <hr /> | |
233 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
234 | </DIV> | |
235 | <!--End of Navigation Panel--> | |
236 | <ADDRESS> | |
237 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
238 | </ADDRESS> | |
239 | </BODY> | |
240 | </HTML> |