第一步先下载源码,解压后 ./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();
Db* pdb; try {
env.set_error_stream(&cerr);
env.open("/Users/wangyi/db", DB_CREATE | DB_INIT_MPOOL, ); pdb = new Db(&env, );
// 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, ); Dbt key(const_cast<char*>(fruit.data()), fruit.size());
Dbt value(const_cast<char*>(apple.data()), apple.size()+);
pdb->put(NULL, &key, &value, ); Dbt value_orange(const_cast<char*>(orange.data()), orange.size()+);
pdb->put(NULL, &key, &value_orange, ); // 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[];
Dbt data;
data.set_data(buffer);
data.set_ulen();
data.set_flags(DB_DBT_USERMEM);
if (pdb->get(NULL, &key, &data, ) == DB_NOTFOUND) {
cerr << "Not found" << endl;
} else {
cout << "Found: " << buffer << endl;
} if (pdb != NULL) {
pdb->close();
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, );
pdb->remove("/Users/wangyi/db/access.db", NULL, );
delete pdb;
}
env.close();
} catch (DbException& e) {
cerr << "DbException: " << e.what() << endl;
return -;
} catch (std::exception& e) {
cerr << e.what() << endl;
return -;
} return ;
}

编译:

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

最新文章

  1. 为C# as 类型转换及Assembly.LoadFrom埋坑!
  2. C语言 &#183; 回文数
  3. 让 select 的 option 标签支持事件监听(如复制操作)
  4. ural 1106,二分图染色,DFS
  5. PHPStorm怎么修改选中的背景颜色呢?
  6. Android Studio--学习系列(1)
  7. VC/MFC强制退出本进程自己,VC/MFC关闭自己
  8. codeforces 192A Funky Numbers
  9. java hascode
  10. SpringMvc(4-1)Spring MVC 中的 forward 和 redirect
  11. WebPack命令执行的时候,其内部处理逻辑是什么
  12. k8s 网络模型
  13. SQL Server 2008 开启远程连接
  14. flask 处理表单数据
  15. jenkins+php+svn快速部署测试环境开发环境快速部署
  16. python多进程(三)
  17. LCD相关基础知识
  18. Ubuntu 14.10安装simplescalar
  19. python之json扩展
  20. 洛谷P4014 分配问题(费用流)

热门文章

  1. hdu 4965 矩阵快速幂 矩阵相乘性质
  2. Yii 之cookie的使用
  3. hdu1569 方格取数 求最大点权独立集
  4. python学习之-requests模块基础
  5. Servlet(生命周期)
  6. SSH login without password
  7. outlook 2010 自动密送Email
  8. Android studio 导入githubproject
  9. Dos 改动IP 地址
  10. Linux下VLAN功能的实现 (转)