使用Berkeley DB(bsddb模块),Python

时间:2021-12-01 23:17:40

I'm using python 2.7.3 and and Berkeley DB to store data. I didn't find much information about that module, only in python docks. I saw there some function described, but I didn't see instruction on how to delete a record from database. Help please, if you know how to delete a record and is that possible using bsddb ?

我正在使用python 2.7.3和Berkeley DB来存储数据。我没有找到关于该模块的更多信息,仅在python docks中。我看到有一些功能描述,但我没有看到如何从数据库中删除记录的说明。请帮助,如果您知道如何删除记录,那可能使用bsddb吗?

1 个解决方案

#1


2  

According to the documentation:

根据文件:

Once instantiated, hash, btree and record objects support the same methods as dictionaries.

实例化后,hash,btree和record对象支持与字典相同的方法。

So, you can use del db_object['key'] to delete specific record like a dictionary.

因此,您可以使用del db_object ['key']删除特定记录,如字典。

>>> import bsddb
>>> db = bsddb.hashopen('a.db', 'c')
>>> db['a'] = '1'
>>> db.keys()
['a']
>>> del db['a'] # <-----
>>> db.keys()
[]

db_object.pop('key') also works.

db_object.pop('key')也可以。

>>> db['b'] = '2'
>>> db.keys()
['b']
>>> db.pop('b')
'2'

del, .pop() with non-existing key will raise KeyError or similar exception. If you want ignore non-existing key, use .pop('key', None):

del,.pop()与不存在的键将引发KeyError或类似的异常。如果要忽略不存在的密钥,请使用.pop('key',None):

>>> db.pop('b') # This raises an exception
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/_abcoll.py", line 497, in pop
    value = self[key]
  File "/usr/lib/python2.7/bsddb/__init__.py", line 270, in __getitem__
    return _DeadlockWrap(lambda: self.db[key])  # self.db[key]
  File "/usr/lib/python2.7/bsddb/dbutils.py", line 68, in DeadlockWrap
    return function(*_args, **_kwargs)
  File "/usr/lib/python2.7/bsddb/__init__.py", line 270, in <lambda>
    return _DeadlockWrap(lambda: self.db[key])  # self.db[key]
KeyError: 'b'
>>> db.pop('b', None) # This does not.
>>>

#1


2  

According to the documentation:

根据文件:

Once instantiated, hash, btree and record objects support the same methods as dictionaries.

实例化后,hash,btree和record对象支持与字典相同的方法。

So, you can use del db_object['key'] to delete specific record like a dictionary.

因此,您可以使用del db_object ['key']删除特定记录,如字典。

>>> import bsddb
>>> db = bsddb.hashopen('a.db', 'c')
>>> db['a'] = '1'
>>> db.keys()
['a']
>>> del db['a'] # <-----
>>> db.keys()
[]

db_object.pop('key') also works.

db_object.pop('key')也可以。

>>> db['b'] = '2'
>>> db.keys()
['b']
>>> db.pop('b')
'2'

del, .pop() with non-existing key will raise KeyError or similar exception. If you want ignore non-existing key, use .pop('key', None):

del,.pop()与不存在的键将引发KeyError或类似的异常。如果要忽略不存在的密钥,请使用.pop('key',None):

>>> db.pop('b') # This raises an exception
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/_abcoll.py", line 497, in pop
    value = self[key]
  File "/usr/lib/python2.7/bsddb/__init__.py", line 270, in __getitem__
    return _DeadlockWrap(lambda: self.db[key])  # self.db[key]
  File "/usr/lib/python2.7/bsddb/dbutils.py", line 68, in DeadlockWrap
    return function(*_args, **_kwargs)
  File "/usr/lib/python2.7/bsddb/__init__.py", line 270, in <lambda>
    return _DeadlockWrap(lambda: self.db[key])  # self.db[key]
KeyError: 'b'
>>> db.pop('b', None) # This does not.
>>>