Skip to content

Instantly share code, notes, and snippets.

@opamp
Created June 21, 2011 16:41
Show Gist options
  • Save opamp/1038280 to your computer and use it in GitHub Desktop.
Save opamp/1038280 to your computer and use it in GitHub Desktop.
practice_libdb_cxx
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<db_cxx.h>
using namespace std;
//BerkeleyDBの練習...
//db51のlibdb_cxx.aなどとリンクする必要があります。
class Myclass{
public:
Myclass(){
a = "opamp ";
b = 0;
}
string a;
int b;
};
int main(int argc,char* argv[]){
Db db(NULL,0);
u_int32_t oFlags = DB_CREATE;
try{
db.open(NULL,"mydb.db",NULL,DB_BTREE,oFlags,0);
}
catch(DbException &e){
return 1;
}catch(exception &e){
return 1;
}
Myclass abc;
Myclass* def;
abc.a += "OPAMP";
abc.b = 1000;
int k = 0;
Dbt key(&k,sizeof(int));
Dbt data(&abc,sizeof(Myclass));
Dbt rdata;
int ret = db.put(NULL,&key,&data,DB_NOOVERWRITE);
if(ret == DB_KEYEXIST){ //すでにkeyが存在したら
cout<<"read DB"<<endl;
rdata.set_data(def); //この行はいらないかもしれない
rdata.set_ulen(sizeof(Myclass));
db.get(NULL,&key,&rdata,0);
def = (Myclass*)rdata.get_data();
cout<<"="<<endl<<
def->a<<endl<<
def->b<<endl<<endl;
cout<<"EXIT"<<endl;
}
db.close(0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment