博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BDB c++例子,从源码编译到运行
阅读量:6593 次
发布时间:2019-06-24

本文共 2631 字,大约阅读时间需要 8 分钟。

  • --enable-cxx

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

 

默认的安装路径是:

/usr/local/BerkeleyDB.6.1/

 

代码如下:

#include 
#include
#include
#include
#include
#include
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
(fruit.data()), fruit.size()); Dbt value(const_cast
(apple.data()), apple.size()+1); pdb->put(NULL, &key, &value, 0); Dbt value_orange(const_cast
(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

本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6524504.html,如需转载请自行联系原作者

你可能感兴趣的文章
Python web 框架 - Django入门
查看>>
换种思路解决Linux -> windows的自动部署
查看>>
gfs2共享集群存储
查看>>
Badboy自动化测试工具9 查看回放结果
查看>>
重拾JAVA之WinForm实战之(三)
查看>>
分布式服务框架-原理与实践:11---服务灰度发布【扩展】-蓝绿发布-续
查看>>
安装php 插件 扩展
查看>>
如何在Sky Drive上创建文档、文件夹
查看>>
招聘信息NO.4
查看>>
线性表之栈与队列
查看>>
一次复杂的JavaScript拼凑
查看>>
Git撤销修改
查看>>
项目管理实施流程(八)系统运维
查看>>
ipv4及ipv6及tcp的头部结构
查看>>
win7图片目录位置
查看>>
Maven
查看>>
FastDFS之集群部署
查看>>
Centos6.3系统语言设置
查看>>
我的友情链接
查看>>
javascript不可用的问题探究
查看>>