00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <db_cxx.h>
00018 #include <iostream.h>
00019
00020 int main(int argc, char *argv[])
00021 {
00022 try {
00023 Db *db = new Db(NULL, 0);
00024 db->open(NULL, "my.db", NULL, DB_BTREE, DB_CREATE, 0644);
00025
00026
00027
00028
00029
00030 Dbt *keydbt = new Dbt((char*)"key", 4);
00031 Dbt *datadbt = new Dbt((char*)"data", 5);
00032 db->put(NULL, keydbt, datadbt, 0);
00033
00034
00035
00036 Dbt *goodkeydbt = new Dbt((char*)"key", 4);
00037 Dbt *badkeydbt = new Dbt((char*)"badkey", 7);
00038 Dbt *resultdbt = new Dbt();
00039 resultdbt->set_flags(DB_DBT_MALLOC);
00040
00041 int ret;
00042
00043 if ((ret = db->get(NULL, goodkeydbt, resultdbt, 0)) != 0) {
00044 cout << "get: " << DbEnv::strerror(ret) << "\n";
00045 }
00046 else {
00047 char *result = (char *)resultdbt->get_data();
00048 cout << "got data: " << result << "\n";
00049 }
00050
00051 if ((ret = db->get(NULL, badkeydbt, resultdbt, 0)) != 0) {
00052
00053 cout << "get using bad key: "
00054 << DbEnv::strerror(ret) << "\n";
00055 }
00056 else {
00057 char *result = (char *)resultdbt->get_data();
00058 cout << "*** got data using bad key!!: "
00059 << result << "\n";
00060 }
00061
00062
00063 cout << "truncating data...\n";
00064 u_int32_t nrecords;
00065 db->truncate(NULL, &nrecords, 0);
00066 cout << "truncate returns " << nrecords << "\n";
00067 if ((ret = db->get(NULL, goodkeydbt, resultdbt, 0)) != 0) {
00068
00069 cout << "after truncate get: "
00070 << DbEnv::strerror(ret) << "\n";
00071 }
00072 else {
00073 char *result = (char *)resultdbt->get_data();
00074 cout << "got data: " << result << "\n";
00075 }
00076
00077 db->close(0);
00078 cout << "finished test\n";
00079 }
00080 catch (DbException &dbe) {
00081 cerr << "Db Exception: " << dbe.what();
00082 }
00083 return 0;
00084 }