BDB c++例子,从源码编译到运行
2024-08-30 02:06:57
第一步先下载源码,解压后 ./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
最新文章
- 为C# as 类型转换及Assembly.LoadFrom埋坑!
- C语言 &#183; 回文数
- 让 select 的 option 标签支持事件监听(如复制操作)
- ural 1106,二分图染色,DFS
- PHPStorm怎么修改选中的背景颜色呢?
- Android Studio--学习系列(1)
- VC/MFC强制退出本进程自己,VC/MFC关闭自己
- codeforces 192A Funky Numbers
- java hascode
- SpringMvc(4-1)Spring MVC 中的 forward 和 redirect
- WebPack命令执行的时候,其内部处理逻辑是什么
- k8s 网络模型
- SQL Server 2008 开启远程连接
- flask 处理表单数据
- jenkins+php+svn快速部署测试环境开发环境快速部署
- python多进程(三)
- LCD相关基础知识
- Ubuntu 14.10安装simplescalar
- python之json扩展
- 洛谷P4014 分配问题(费用流)