c - berkeley db 保存到磁盘

标签 c berkeley-db

我是 berkeley db 的新手,当我以这种方式创建数据库时:

DB *dbp;

db_create(&dbp, NULL, 0);


dbp->put(dbp, NULL, &key, &data, 0);

我是否将数据存储到磁盘中?如果是这样,数据库文件在哪里?据我所知,只有当我使用 DB_EN​​V->open() 创建数据库环境并指定参数 char *db_home 时,我才会存储到一个真实的文件中,我我对吗?非常感谢您的宝贵时间。

最佳答案

来自 Berkeley DB 规范;

To open a database, you must first use the db_create() function to initialize a DB handle. Once you have initialized the DB handle, you use its open() method to open the database. Note that by default, DB does not create databases if they do not already exist. To override this behavior, specify the DB_CREATE flag on the open() method.

#include <db.h> 

//...

DB *dbp;           /* DB structure handle */
u_int32_t flags;   /* database open flags */
int ret;           /* function return value */

/* Initialize the structure. This
 * database is not opened in an environment, 
 * so the environment pointer is NULL. */
ret = db_create(&dbp, NULL, 0);
if (ret != 0) {
  /* Error handling goes here */
}

/* Database open flags */
flags = DB_CREATE;    /* If the database does not exist, 
                       * create it.*/

/* open the database */
ret = dbp->open(dbp,        /* DB structure pointer */
                NULL,       /* Transaction pointer */
                "my_db.db", /* On-disk file that holds the database. */
                NULL,       /* Optional logical database name */
                DB_BTREE,   /* Database access method */
                flags,      /* Open flags */
                0);         /* File mode (using defaults) */
if (ret != 0) {
  /* Error handling goes here */
}

https://docs.oracle.com/cd/E17076_05/html/gsg/C/databases.html#DBOpen

关于c - berkeley db 保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36947325/

相关文章:

c++ - 错误 - 当前位置没有可用的源代码

svn - 如何确定我拥有的 SVN 存储库类型?

c - 静态函数对另一个文件可见

c - gcc 未能警告未初始化的变量

c - 将 memcpy 与具有常量成员的目标结构一起使用是否合法?

centos - Rpmdb 损坏

c - 如何在没有 GUI 的 Linux 中查看“.db 文件

c - for循环为什么循环出的变量是 "last index"+ step?

c - 下面的输出如何可能?

svn - 如何确定是否使用BDB或fsfs后端创建了Subversion存储库?