【发布时间】:2020-10-08 15:03:01
【问题描述】:
所以这听起来像是一个愚蠢的问题,但我已经查阅了 Berkeley DB C++ API 的大部分官方文档,但似乎找不到答案。如何检查数据库中是否存在密钥?这是一些示例代码。
// Database created.
Dbt key(&some_string, some_size);
Dbt data(&some_struct, some_other_size);
database.put(nullptr, &key, &data, 0);
// Now my &some_string key is definitely in the database, and I can easily get it.
// However if I create an identical string to search for the key, I get DB_NOTFOUND
Dbt search_key(&the_same_string, the_same_size);
int ret = database.exists(nullptr, &search_key, 0);
// This returns DB_NOTFOUND.
如果我传递原始键 Dbt 对象,那么 ret 当然是 0。但是如果您还没有与该确切记录相关联的 Dbt 对象,是否无法检查键是否存在?
如果我设置了 DB_FIRST 标志,我也尝试过使用游标和 Dbc::get() 来查找键。但是那个会自动写入我传递的任何数据 Dbt 对象。
我要做的就是检查是否存在具有特定键的 Dbt 对象,仅此而已。
【问题讨论】:
标签: c++ berkeley-db