首页 > 编程语言 >BDB c++例子,从源码编译到运行

BDB c++例子,从源码编译到运行

时间:2023-05-30 22:35:04浏览次数:49  
标签:NULL BDB db DB c++ 源码 include data pdb

第一步先下载源码,解压后 ./dist/configure --enable-cxx编译,然后make, make install

  • --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

标签:NULL,BDB,db,DB,c++,源码,include,data,pdb
From: https://blog.51cto.com/u_11908275/6382341

相关文章

  • MongoDB C++ gridfs worked example
    使用libmongoc,参考:http://mongoc.org/libmongoc/current/mongoc_gridfs_t.html#include<mongoc.h>#include<stdio.h>#include<stdlib.h>#include<fcntl.h>classMongoGridFS{public:MongoGridFS(constchar*db);~MongoGridFS();......
  • mongodb c++ driver安装踩坑记
     安装教程:https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/(1)“initializer_list”filenotfoundhttp://stackoverflow.com/questions/19493671/initializer-list-no-such-file-or-directorySinceyouareusing GCC-4.8 andyourproblemisthatyoud......
  • 基于JAVA的springboot+vue学生综合测评系统,附源码+数据库+论文+PPT
    1、项目介绍本学生综合测评系统以springboot作为框架,b/s模式以及MySql作为后台运行的数据库,同时使用Tomcat用为系统的服务器。本系统主要包括首页,个人中心,学生管理,试题信息管理,测评试题管理,管理员管理,综合测评管理,系统管理,综合考试管理等功能,通过这些功能的实现基本能够满足日常......
  • 单链表(c++实现)
    template<typenameT>classListNode{public:explicitListNode(Tvalue_,ListNode*next_=nullptr):value(value_),next(next_){}TgetValue()const{returnvalue;}ListNode<T>*getNext()const{returnnext;};voidsetNext(ListNo......
  • 内存泄漏、缓存溢出?C和C++,哪个更懂得管理内存质量?
    一、c/c++程序内存区域划分c和c++的内存区域划分是十分相似的,因为c++是完全兼容c语言,是c语言的面向对象的升级版。接下来看如下图:程序的内存区域被划分成6个区域。内核空间、栈、内存映射段、堆、数据段、代码段。下面是对相关内存区域名词解释:栈又叫堆栈--非静态局部变量/函数参数......
  • 为什么 C++ 有指针了还要引用
    引用传递,只是明面上,没有使用值传递,值传递本身是不可避免的。编译器,暗地里通过指针(或者其他可以替代指针的数据类型)的值传递,替换了引用传递。所以引用传递,实质上是地址传递,别名这东西只是概念,是一种抽象,别名是没法传递的。别名,可不是真实的数据类型。因为,函数传递参数需要,数据复制,......
  • springboot启动源码
    每个SpringBoot项目都有一个主程序启动类,在主程序启动类中有一个启动项目的main()方法,在该方法中通过执行SpringApplication.run()即可启动整个SpringBoot程序。问题:那么SpringApplication.run()方法到底是如何做到启动SpringBoot项目的呢?下面我们查看run()方法内部的源码,核......
  • UE4 源码解析----引擎初始化流程
      在研究UE4的源码过程中着实不理解的地方有很多,今天给大家分享一下UE4引擎的初始化流程。一、引擎的函数入口C++的函数入口都是Main()函数入口,UE4也是一样,Engine\Source\Runtime\Launch\PrivateWindows函数入口 引擎入口函数为:GuardedMain 二、引擎初始化的三个阶......
  • C++中模拟split
    #include<iostream>#include<sstream>usingnamespacestd;intmain(){ stringstr; getline(cin,str); istringstreamin(str); stringa; while(getline(in,a,'*')){ cout<<a<<''; } return0;}123*456*789123......
  • 蓝桥杯 基础练习 特殊回文数(C++)
    资源限制内存限制:512.0MBC/C++时间限制:1.0sJava时间限制:3.0sPython时间限制:5.0s问题描述123321是一个非常特殊的数,它从左边读和从右边读是一样的。输入一个正整数n,编程求所有这样的五位和六位十进制数,满足各位数字之和等于n。输入格式输入一行,包含一个正整......