BDB C++ API sample

来源:互联网 发布:mac itunes 同步铃声 编辑:程序博客网 时间:2024/05/16 07:35

This is a sample of C++ access Oracle Berkeley DB 12cR1 (12.1.6.1.26)


1. Download BDB

    http://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html


2.  Build

     $ tar -zxvf db-6.1.26.gz

     $ cd db-6.1.26/build_unix

     $ ../dist/configure --prefix=/root/to/install/path --enable-cxx

     $ make

     $ make install


3. Prepare program execution environment

     $ export BDB_HOME=/root/to/install/path

     $ export LD_LIBRARY_PATH=${BDB_HOME}/lib:${LD_LIBRARY_PATH}


4. Source file


#include <stdlib.h>#include <string.h>#include <iostream>#include <iomanip>#include <string> #include "db_cxx.h" #define COUNT 3int main() {    std::string names[COUNT]  = { "John", "Jim", "Bob" };    std::string states[COUNT] = { "Massachusetts", "Washington, D.C.", "New York" };    try {        Db * pdb;        pdb = new Db(NULL, 0);        pdb->open(NULL, "data/people.db", NULL, DB_BTREE, DB_CREATE | DB_TRUNCATE, 0);         // insert        {            for (int i = 0; i < COUNT; i++) {                Dbt key(const_cast<char*>(names[i].data()), names[i].size());                Dbt value(const_cast<char*>(states[i].data()), states[i].size()+1);                pdb->put(NULL, &key, &value, 0);            }        }         // query key        {            Dbt key(const_cast<char*>(names[1].data()), names[1].size());            char buffer[1024];            Dbt data;            data.set_data(buffer);            data.set_ulen(1024);            data.set_flags(DB_DBT_USERMEM);            if (pdb->get(NULL, &key, &data, 0) == DB_NOTFOUND) {                std::cerr << "Not found" << std::endl;                return -1;            } else {                std::cout << "Found: [" << buffer << "]" << std::endl;            }        }         // delete key        {            std::string toDelete = "Jim";            Dbt key(const_cast<char *>(toDelete.data()), toDelete.size());            if (pdb->del(NULL, &key, 0) != 0) {                std::cerr << "Not found" << std::endl;                return -1;            }            else {                std::cout << "Delete success: " << toDelete << std::endl;            }        }        //traverse        {            Dbc *dbc = NULL;            Dbt key, data;            char bufferkey[1024] = { '\0' };            char bufferdata[1024] = { '\0' };            key.set_data(bufferkey);            key.set_ulen(1024);            key.set_flags(DB_DBT_USERMEM);            data.set_data(bufferdata);            data.set_ulen(1024);            data.set_flags(DB_DBT_USERMEM);            pdb->cursor(NULL, &dbc, 0);            while (dbc->get(&key, &data, DB_NEXT) == 0) {                std::cout << "key[" << (char *)key.get_data() << "]=[" << (char *)data.get_data() << "]" << std::endl;            }            dbc->close();        }        // delete database        {            pdb->close(0);  // DB->remove: method not permitted after handle's open method            delete pdb;            pdb = new Db(NULL, 0);            pdb->remove("data/people.db", NULL, 0);            delete pdb;        }    } catch (DbException& e) {        std::cerr << "DbException: " << e.what() << std::endl;        return -1;    } catch (std::exception& e) {        std::cerr << e.what() << std::endl;        return -1;    }    return 0;}


5. Compile Source File

    $ g++ -o main -I${BDB_HOME}/include main.cpp -L${BDB_HOME}/lib -ldb_cxx


6. Run Program

   $ ./main 

   Found: [Washington, D.C.]

   Delete success: Jim

   key[Bob]=[New York]

   key[John]=[Massachusetts]

   $ 


7. Thank You.

0 0
原创粉丝点击