How to retrieve all the records from a Berkeley DB in Ruby

Posted by Federico Builes on Stack Overflow See other posts from Stack Overflow or by Federico Builes
Published on 2009-02-23T12:12:29Z Indexed on 2010/03/21 11:01 UTC
Read the original article Hit count: 705

Filed under:
|
|

I'd like to be able to get all the key-values stored in a Berkeley DB using the Ruby bindings from http://github.com/mattbauer/bdb/tree/master but I'm not sure how to proceed. Any pointers will be appreciated.

UPDATE

Here's a small script that loops over the keys and prints them. Based on Pax' answer:

require 'rubygems'
require 'bdb'

env = Bdb::Env.new(0)
env.open('foo', Bdb::DB_CREATE,0)

db = env.db
db.open(nil, 'db1.db', nil, Bdb::Db::BTREE, Bdb::DB_CREATE,0)

db.put(nil, 'key',  'value',  0)
db.put(nil, 'key1', 'value1', 0)
db.put(nil, 'key2', 'value2', 0)

dbc = db.cursor(nil,0)
key,val = dbc.get(nil,nil,Bdb::DB_FIRST)
while key
  p key,val
  key,val = dbc.get(nil,nil,Bdb::DB_NEXT)
end
dbc.close
db.close(0)
env.close

© Stack Overflow or respective owner

Related posts about ruby

Related posts about berkeley-db