我正在尝试使用 Python 3.5 和 bsddb3 将数据写入 Berkeley DB 数据库。我对 Berkeley DB 没有任何经验,但我喜欢使用时间戳作为键,并使用一些 JSON 数据作为各自的值。以下示例导致错误:
#!/usr/bin/env python3
import bsddb3
import time
fn = 'berkeley.db'
db = bsddb3.hashopen(fn, 'c')
t = time.time()
d = "{ data: 'foo' }"
db[t] = d
db.close()
错误:
$ python3 example.py
[...]
self.db[key] = value
TypeError: Bytes or Integer object expected for key, float found
time.time()
例如,使用整数而不是db[0] = data
, 也不起作用:TypeError: Integer keys only allowed for Recno and Queue DB's
.
不幸的是,没有关于如何在 Python 中使用 Berkeley DB 的全面文档。关于我做错了什么的任何建议?