DB_FIRST moves the cursor to the first entry in the database. It looks like you want to do cur.pget('letter', db.DB_SET).<br><br><br>
<div class="gmail_quote">On Tue, Dec 2, 2008 at 12:46 PM, <a href="mailto:denis.papathanasiou@gmail.com">denis.papathanasiou@gmail.com</a> <span dir="ltr"><<a href="mailto:denis.papathanasiou@gmail.com">denis.papathanasiou@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">I've got a simple hash saved as a Berkeley DB file.<br><br>The keys are: ['a', 'b', '0', 'c', '1', 'd', '2'] and the values are:<br>
['letter', 'letter', 'number', 'letter', 'number', 'letter', 'number'].<br><br>I created the db with a secondary index, so that I can lookup by value,<br>e.g. call pget('letter') and get a list of ['a', 'b', 'c', 'd'] as the<br>
result.<br><br>Except, it doesn't work; in that example, pget('letter') returns just 'a'.<br><br>Then I read in the Oracle docs that secondary indices with duplicates<br>need to use the cursor form of pget().<br>
<br>But when I tried that, I got back the entire db.<br><br>Any suggestions?<br><br>Or are there examples of using the cursor form of pget() in bsddb3 I<br>could look at?<br><br>Here's my code:<br><br>from bsddb3 import db<br>
<br>pdb_object = db.DB()<br>pdb_object.open("alphanumeric.db", None, db.DB_BTREE, db.DB_CREATE)<br>sdb_object = db.DB()<br>sdb_object.set_flags(db.DB_DUP | db.DB_DUPSORT)<br>sdb_object.open("alphanumeric_secondary.db", None, db.DB_BTREE,<br>
db.DB_CREATE)<br>pdb_object.associate(sdb_object, (lambda primary_key, primary_data:<br>primary_data))<br><br>pdb_object['a'] = 'letter'<br>pdb_object['b'] = 'letter'<br>pdb_object['0'] = 'number'<br>
pdb_object['c'] = 'letter'<br>pdb_object['1'] = 'number'<br>pdb_object['d'] = 'letter'<br>pdb_object['2'] = 'number'<br><br># call pget():<br><br>>>> sdb_object.pget('letter')<br>
('a', 'letter')<br># returns just one value, not all of them...<br><br><br># call pget() with a cursor:<br><br>cur = sdb_object.cursor()<br>rec = cur.pget('letter', db.DB_FIRST)<br>>>> rec<br>
('letter', 'a', 'letter')<br># so far so good...<br><br>>>> while rec:<br>... rec = cur.pget('letter', db.DB_NEXT)<br>... rec<br>...<br>('letter', 'b', 'letter')<br>
('letter', 'c', 'letter')<br>('letter', 'd', 'letter')<br>('number', '0', 'number')<br>('number', '1', 'number')<br>('number', '2', 'number')<br>
<br># why were 'number' types returned? It looks like the pget() key was<br>ignored...<br><br>_______________________________________________<br>pybsddb mailing list<br><a href="mailto:pybsddb@argo.es">pybsddb@argo.es</a><br>
<a href="http://mailman.argo.es/listinfo/pybsddb" target="_blank">http://mailman.argo.es/listinfo/pybsddb</a><br><a href="http://www.argo.es/~jcea/programacion/pybsddb.htm" target="_blank">http://www.argo.es/~jcea/programacion/pybsddb.htm</a><br>
</blockquote></div><br>