BDB C++ API sample
来源:互联网 发布:mac itunes 同步铃声 编辑:程序博客网 时间:2024/05/16 07:35
This is a sample of C++ access Oracle Berkeley DB 12cR1 (12.1.6.1.26)
1. Download BDB
http://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html
2. Build
$ tar -zxvf db-6.1.26.gz
$ cd db-6.1.26/build_unix
$ ../dist/configure --prefix=/root/to/install/path --enable-cxx
$ make
$ make install
3. Prepare program execution environment
$ export BDB_HOME=/root/to/install/path
$ export LD_LIBRARY_PATH=${BDB_HOME}/lib:${LD_LIBRARY_PATH}
4. Source file
#include <stdlib.h>#include <string.h>#include <iostream>#include <iomanip>#include <string> #include "db_cxx.h" #define COUNT 3int main() { std::string names[COUNT] = { "John", "Jim", "Bob" }; std::string states[COUNT] = { "Massachusetts", "Washington, D.C.", "New York" }; try { Db * pdb; pdb = new Db(NULL, 0); pdb->open(NULL, "data/people.db", NULL, DB_BTREE, DB_CREATE | DB_TRUNCATE, 0); // insert { for (int i = 0; i < COUNT; i++) { Dbt key(const_cast<char*>(names[i].data()), names[i].size()); Dbt value(const_cast<char*>(states[i].data()), states[i].size()+1); pdb->put(NULL, &key, &value, 0); } } // query key { Dbt key(const_cast<char*>(names[1].data()), names[1].size()); 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) { std::cerr << "Not found" << std::endl; return -1; } else { std::cout << "Found: [" << buffer << "]" << std::endl; } } // delete key { std::string toDelete = "Jim"; Dbt key(const_cast<char *>(toDelete.data()), toDelete.size()); if (pdb->del(NULL, &key, 0) != 0) { std::cerr << "Not found" << std::endl; return -1; } else { std::cout << "Delete success: " << toDelete << std::endl; } } //traverse { Dbc *dbc = NULL; Dbt key, data; char bufferkey[1024] = { '\0' }; char bufferdata[1024] = { '\0' }; key.set_data(bufferkey); key.set_ulen(1024); key.set_flags(DB_DBT_USERMEM); data.set_data(bufferdata); data.set_ulen(1024); data.set_flags(DB_DBT_USERMEM); pdb->cursor(NULL, &dbc, 0); while (dbc->get(&key, &data, DB_NEXT) == 0) { std::cout << "key[" << (char *)key.get_data() << "]=[" << (char *)data.get_data() << "]" << std::endl; } dbc->close(); } // delete database { pdb->close(0); // DB->remove: method not permitted after handle's open method delete pdb; pdb = new Db(NULL, 0); pdb->remove("data/people.db", NULL, 0); delete pdb; } } catch (DbException& e) { std::cerr << "DbException: " << e.what() << std::endl; return -1; } catch (std::exception& e) { std::cerr << e.what() << std::endl; return -1; } return 0;}
5. Compile Source File
$ g++ -o main -I${BDB_HOME}/include main.cpp -L${BDB_HOME}/lib -ldb_cxx
6. Run Program
$ ./main
Found: [Washington, D.C.]
Delete success: Jim
key[Bob]=[New York]
key[John]=[Massachusetts]
$
7. Thank You.
- BDB C++ API sample
- BDB C# API初探
- 七、BDB JE与BDB Java API
- BDB(C)编译
- bdb
- bdb
- Api Hijacking (sample included
- Google API Sample
- google map api sample
- Sample of API FND_PROFILE
- BDB(C) Getting Started - Introduction to Berkeley DB
- Berkeley DB (BDB) C语言简单示例
- (C#)Singleton design pattern sample
- lseek() sample program in C
- (C language Sample ) Compile procedure
- OBIEE 12c SAMPLE VM
- sample api for xml简单示例
- Sample of QuickTime API on Windows
- BDB concurrent access
- [学习笔记]MySql存储过程学习二
- Python基本数据类型的注意事项
- 简易学生管理系统(IO流、文件、)
- VNC服务
- BDB C++ API sample
- HAXM is not installed
- Android 6.0 权限管理
- SQL注入攻击以及防护
- 使用JavaFX设置一个简单日历
- c语言模拟实现strcpy函数
- 运算符优先级和结合性
- 根据姓名查询年龄是否超过100
- 机动车缉查布控即席分析引擎