fredrik at pythonware
Oct 17, 1999, 4:44 AM
Post #3 of 3
Thomas Weholt <thomas [at] bibsyst> wrote:
> I try to do something like :
> >>>import shelve
> >>>db = shelve.open('2')
> >>> db['1'] = 'test'
> And quit python, restart it and this happens when I try to read the data
> back in :
> >>> import shelve
> >>> db = shelve.open('2')
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> File "/usr/lib/python1.5/shelve.py", line 152, in open
> return DbfilenameShelf(filename, flag)
> File "/usr/lib/python1.5/shelve.py", line 142, in __init__
> Shelf.__init__(self, anydbm.open(filename, flag))
> File "/usr/lib/python1.5/anydbm.py", line 83, in open
> raise error, "db type could not be determined"
> anydbm.error: db type could not be determined
not sure -- two possible causes comes to mind:
1) you didn't close the database. I have a vague
memory that one of the database drivers won't
properly flush changes to the file if you don't
(cannot remember which one, though).
2) you don't have any database handlers on your
machine, so Python falls back to the "dumbdbm"
driver. and while "anydbm" can fall back on the
"dumbdbm" module when creating a database, it
fails to recognize it when you reopen it...
(guess someone should fix "whichdb"...)
one way around this is to use shelve with an
explicit database, like this:
def gdbm_shelve(filename, flag="c"):
return shelve.Shelf(gdbm.open(filename, flag))
db = gdbm_shelve("dbfile")
(taken from the eff-bot library guide, see below).
<!-- coming monday:
(the eff-bot guide to) the standard python library. -->