0

How do I read Berkeley DB files with Python?

I have this file ...

    [root@dhcp-idev1 ndb]# file dhcp.ndb 
    dhcp.ndb: Berkeley DB (Btree, version 9, native byte-order)

... so I figure I can do this ...

    [root@dhcp-idev1 ndb]# python2.3 
    Python 2.3.4 (#1, Jul 16 2009, 07:01:37) 
    [GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import anydbm
    >>> anydbm.open( './dhcp.ndb' )

... but I get this error message ...

    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "/usr/lib/python2.3/anydbm.py", line 80, in open
        raise error, "db type could not be determined"
    anydbm.error: db type could not be determined
    >>> 

... what am I doing wrong?

1
  • have you tried using dbm.open, gdbm.open or dbhash.open directly? Also, the dbm backend adds a .db extension to the given file name, so try to rename your file to dhcp.db and open it with dbm.open('dhcp'). Commented Apr 8, 2014 at 20:46

2 Answers 2

1

Here Is the code related to this error From anydbm.py

from whichdb import whichdb
  result=whichdb(file)
  if result is None:
     # db doesn't exist
     if 'c' in flag or 'n' in flag:
        # file doesn't exist and the new
        # flag was used so use default type
        mod = _defaultmod
     else:
        raise error, "need 'c' or 'n' flag to open new db"
  elif result == "":
     # db type cannot be determined
     raise error, "db type could not be determined"

If whichdb can open the file but cannot determine the library to use, it returns the empty string.

so question is why its not able to determine the library. May be library required to open this DB file is not installed.

 anydbm is a generic interface to variants of the DBM database — dbhash (requires 
 bsddb), gdbm, or dbm. If none of these modules is installed, the slow-but-simple
 implementation in module dumbdbm will be used.

So either you are missing dumbdbm module(import it and use it instead of anydbm) or you need to install other libraries dbhash gdbm, dbm to open the file.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer! AFAIK I have dumbdb installed.
dumbdbm should be there in python 2.X. Just try using dumbdbm.open( './dhcp.ndb' ). For python 3.X use dbm.dumb.open(filename) . dont forget to import dumbdbm .
Thanks Arvind that works >>> import dumbdbm >>> dumbdbm.open( './dhcp.ndb' ) {}
0

There are several ways to open a Berkeley database file. To find out what methods are available, you can use:

import bsddb3
print(dir(bsddb3))

To find out which method to use on your file, you can use the Linux "file" command, e.g.:

 file SomeFileName

A clear explanation of how to open a Berkeley DB file is available in the book "Python in a Nutshell": https://www.oreilly.com/library/view/python-in-a/0596001886/re342.html

It should be noted that the Berkeley database is automatically included in Python 2.7, but not in Python 3. With Python 3, the Berkeley DB has to be installed separately. The method for installing the Berkeley DB on Linux depends on the distro. On SUSE I used:

zypper install python3-bsddb3
zypper install python3-bsddb3-devel

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.