HuangJie
HuangJie

Reputation: 1530

berkeley db save into disk

I am a newbie in berkeley db, when I create a db in this way:

DB *dbp;

db_create(&dbp, NULL, 0);


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

Do I store the data into the disk? If so, where is the database file? As far as I know, only when I create a database environment using DB_ENV->open() and specify the parameter char *db_home do I store into a real file, am I right? Many thanks for your time.

Upvotes: 1

Views: 343

Answers (1)

user1788956
user1788956

Reputation:

From Berkeley DB spec;

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

Upvotes: 2

Related Questions