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

  • 相关阅读:
    AutoCompleteTextView 简单用法
    照片颠倒问题及查询摄像头参数问题的解决
    Android Studio-引用jar及so文件
    file新建文件及文件夹
    appcompat_v7报错
    fresco加载本地图片、gif资源
    android根据图片路径显示图片
    sublime text3 及相关的安装
    win 10通过自带IIS搭建ftp
    LCA
  • 原文地址:https://www.cnblogs.com/bonelee/p/6524504.html
Copyright © 2011-2022 走看看