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 cout << "finished test\n";
00062 }
00063 catch (DbException &dbe) {
00064 cerr << "Db Exception: " << dbe.what();
00065 }
00066 return 0;
00067 }