berkeley db保存到磁盘

时间:2016-04-29 20:57:48

标签: c berkeley-db

当我以这种方式创建数据库时,我是伯克利数据库中的新手:

DB *dbp;

db_create(&dbp, NULL, 0);


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

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

1 个答案:

答案 0 :(得分:2)

来自Berkeley DB spec;

  

要打开数据库,必须首先使用db_create()函数初始化数据库句柄。初始化DB句柄后,使用open()方法打开数据库。   请注意,默认情况下,如果数据库尚不存在,则不会创建数据库。要覆盖此行为,请在 open()上指定 DB_CREATE 标志方法。

#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