Berkeley DB (1/2)

Introduction

http://www.oracle.com/technetwork/database/database-technologies/berkeleydb/overview/index.html http://docs.oracle.com/cd/E17076_02/html/api_reference/C/frame_main.html

Database Handle

#include <db.h>

int main(int argc, char* argv[])
{
    // Obtain a database handle
    DB* db = NULL;
    int rc = db_create(&db, NULL, 0);
    if (rc != 0)
    {
        printf("Error creating database handle (return code: %d)", rc);
    }

    // Create a new database.
    // By default open will not create a database if it doesn't
    // exist. The DB_CREATE flag is used to indicate that we
    // want to create the database if it doesn't exist.
    rc = db->open(db,                // database handle
        NULL,                        // NULL because the operation is not part transaction
        "MyFirstBerkeleyDB.db",        // Filepath of the database
        NULL,                        // Only useful if we want to store multiple databases in the file
        DB_BTREE,                    // The type of the database
        DB_CREATE,                    // Create the database if it doesn't exist
        0);                            // Use default file mode (ignored on Windows)
    if (rc != 0)
    {
        printf("Error creating database (return code: %d)", rc);
    }

    // Close the database handle
    if (db != NULL)
    {
        db->close(db, 0);
    }

    return 0;
}

DBT Handle

#include <db.h>
#include <string.h>

int main(int argc, char* argv[])
{
    // Obtain a database handle
    DB* db = NULL;
    int rc = db_create(&db, NULL, 0);
    if (rc != 0)
    {
        printf("Error creating database handle (return code: %d)", rc);
    }

    // Create a new database.
    // By default open will not create a database if it doesn't
    // exist. The DB_CREATE flag is used to indicate that we
    // want to create the database if it doesn't exist.
    rc = db->open(db,                // database handle
        NULL,                        // NULL because the operation is not part transaction
        "MyFirstBerkeleyDB.db",        // Filepath of the database
        NULL,                        // Only useful if we want to store multiple databases in the file
        DB_BTREE,                    // The type of the database
        DB_CREATE,                    // Create the database if it doesn't exist
        0);                            // Use default file mode (ignored on Windows)
    if (rc != 0)
    {
        printf("Error creating database (return code: %d)", rc);
    }

    // Create a DBT structure for the key.
    // The key will be used in the put, get and del operations.
    DBT key;
    memset(&key, 0, sizeof(DBT));
    key.data = "John";
    key.size = strlen(key.data) + 1;

    // We're going to add an entry in the database.
    // Create a DBT structure for the value of the entry we want 
    // to add.
    DBT value;
    memset(&value, 0, sizeof(DBT));
    int age = 34;
    value.data = &age;
    value.size = sizeof(int);

    // Use the put function to add the entry in the database.
    // Each entry in the database is a key/value pair.
    rc = db->put(db,
        NULL,
        &key,
        &value,
        DB_NOOVERWRITE);
    if (rc != 0)
    {
        printf("Error adding item (return code: %d)", rc);
    }

    // Create a new DBT structure to hold the value
    // we're going to read from the database. We know
    // it is an int so we make it just big enough to hold
    // an int.
    DBT dbValue;
    memset(&dbValue, 0, sizeof(DBT));
    int dbAge = 0;
    dbValue.data = &dbAge;
    dbValue.ulen = sizeof(int);
    dbValue.flags = DB_DBT_USERMEM;

    // Read the value associated with the given key
    // from the database
    rc = db->get(db, NULL, &key, &dbValue, 0);
    if (rc != 0)
    {
        printf("Error reading item (return code: %d)", rc);
    }
    else
    {
        printf("The value read from the database is %d\n", dbAge);
    }

    // Delete the entry from the database
    rc = db->del(db, NULL, &key, 0);
    if (rc != 0)
    {
        printf("Error deleting item (return code: %d)", rc);
    }

    // Close the database handle
    if (db != NULL)
    {
        db->close(db, 0);
    }

    return 0;
}

blog comments powered by Disqus

Copyright(c) 2006-2017 Xavier Leclercq | Privacy policy

Home
Contact Us
Search