BDB C++ API sample

这篇博客提供了一个使用C++访问Oracle Berkeley DB 12cR1 (12.1.6.1.26)的示例。包括下载BDB,构建过程,设置环境变量,源代码编译以及运行程序的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 3
int 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值