pythonberkeley-db

How do I read Berkeley DB files with Python?


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?


Solution

  • 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.