Created
June 21, 2011 16:41
-
-
Save opamp/1038280 to your computer and use it in GitHub Desktop.
practice_libdb_cxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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