zoukankan      html  css  js  c++  java
  • BDB c++例子,从源码编译到运行

    • --enable-cxx

      To build the Berkeley DB C++ API, enter --enable-cxx as an argument to configure.

    默认的安装路径是:

    /usr/local/BerkeleyDB.6.1/

    代码如下:

    #include <stdlib.h>
    #include <string.h>
     
    #include <iostream>
    #include <iomanip>
    #include <string>
     
    #include <db_cxx.h>
     
    using namespace std;
     
    const char* kDatabaseName = "access.db";
     
    int main() {
     
      string fruit("fruit");
      string apple("apple");
      string orange("orange");
     
      DbEnv env(0);
      Db* pdb;
     
      try {
        env.set_error_stream(&cerr);
        env.open("/Users/wangyi/db", DB_CREATE | DB_INIT_MPOOL, 0);
     
        pdb = new Db(&env, 0);
        // If you want to support duplicated records and make duplicated
        // records sorted by data, you need to call:
        //   pdb->set_flags(DB_DUPSORT);
        // Note that only Btree-typed database supports sorted duplicated
        // records
     
        // If the database does not exist, create it.  If it exists, clear
        // its content after openning.
        pdb->open(NULL, "access.db", NULL, DB_BTREE, DB_CREATE | DB_TRUNCATE, 0);
     
        Dbt key(const_cast<char*>(fruit.data()), fruit.size());
        Dbt value(const_cast<char*>(apple.data()), apple.size()+1);
        pdb->put(NULL, &key, &value, 0);
     
        Dbt value_orange(const_cast<char*>(orange.data()), orange.size()+1);
        pdb->put(NULL, &key, &value_orange, 0);
     
        // You need to set ulen and flags=DB_DBT_USERMEM to prevent Dbt
        // from allocate its own memory but use the memory provided by you.
        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) {
          cerr << "Not found" << endl;
        } else {
          cout << "Found: " << buffer << endl;
        }
     
        if (pdb != NULL) {
          pdb->close(0);
          delete pdb;
          // You have to close and delete an exisiting handle, then create
          // a new one before you can use it to remove a database (file).
          pdb = new Db(NULL, 0);
          pdb->remove("/Users/wangyi/db/access.db", NULL, 0);
          delete pdb;
        }
        env.close(0);
      } catch (DbException& e) {
        cerr << "DbException: " << e.what() << endl;
        return -1;
      } catch (std::exception& e) {
        cerr << e.what() << endl;
        return -1;
      }
     
      return 0;
    }

    编译:

     g++ -o cass cassandra_demo.cpp -I /usr/local/BerkeleyDB.6.1/include/ -L /usr/local/BerkeleyDB.6.1/lib/ -ldb_cxx-6.1

    运行:

    export LD_LIBRARY_PATH=/usr/local/BerkeleyDB.6.1/lib/

    ./cass

    参考:

    https://docs.oracle.com/cd/E17275_01/html/programmer_reference/build_unix_conf.html

    https://cxwangyi.wordpress.com/2010/10/10/how-to-use-berkeley-db/

    http://stackoverflow.com/questions/2628227/berkeley-db-cant-compile-c-codes

  • 相关阅读:
    WIN10安装python及numpy等第三方库以及卸载
    学习Python一年,基础忘记了,看看面试题回忆回议,Python面试题No3
    包含了 java环境,mysql,nginx,redis docker 镜像
    Docker的镜像制作与整套项目一键打包部署
    RedHat Enterprise Linux 5.8 升级openssl
    RedHat Enterprise Linux 5.8 升级openssl
    RedHat Enterprise Linux 5.8 升级openssl
    log4net进阶手札(二):基本用法
    log4net进阶手札(二):基本用法
    log4net进阶手札(二):基本用法
  • 原文地址:https://www.cnblogs.com/bonelee/p/6524504.html
Copyright © 2011-2022 走看看