#include <stdio.h> #include <stdlib.h>
#include <db.h>
#include <string.h> int main(int argc, char* argv[]) { //declare DB_ENV *ptr_db_env; DB *ptr_db; DBT key, data; u_int32_t env_flags, db_flags; FILE *fptr_db_env_err_log, *fptr_db_err_log; int ret; int sel; int userid; char *username; username = (char*) malloc(100 * sizeof(char)); //initial fptr_db_env_err_log = fopen("./db_env_err.log", "r+"); if (NULL == fptr_db_env_err_log) { printf("Open Enviroment Log File Failed!\n"); } else { printf("Open Enviroment Log File Succeeded!\n"); } ret = db_env_create(&ptr_db_env, 0); if (0 != ret) { printf("Create DB Enviroment Failed!\n"); } else { printf("Create DB Enviroment Succeeded!\n"); ptr_db_env->set_errfile(ptr_db_env, fptr_db_env_err_log); } env_flags = DB_CREATE | DB_INIT_MPOOL; ret = ptr_db_env->open(ptr_db_env, "/tmp/dbs", env_flags, 0); if (0 != ret) { printf("Open DB Enviroment Failed!\n"); } else { printf("Open DB Enviroment Succeeded!\n"); } fptr_db_err_log = fopen("./db_err.log", "r+"); if (NULL == fptr_db_err_log) { printf("Open DB Log File Failed!\n"); } else { printf("Open DB Log File Succeeded!\n"); } ret = db_create(&ptr_db, ptr_db_env, 0); if (0 != ret) { printf("Create DB Failed!\n"); } else { printf("Create DB Succeeded!\n"); ptr_db->set_errfile(ptr_db, fptr_db_err_log); } db_flags = DB_CREATE; ret = ptr_db->open(ptr_db, NULL, "user.db", NULL, DB_HASH, db_flags, 0); if (0 != ret) { printf("Open DB Failed!\n"); } else { printf("Open DB Succeeded!\n"); } //ui memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); while (1) { printf("========Menu========\n"); printf("1:add\n"); printf("2:query\n"); printf("3:delete\n"); printf("4:quit\n"); printf("Your selection:"); scanf("%d", &sel); switch (sel) { case 1: printf("Please input the key:"); scanf("%d", &userid); printf("Please input the name:"); scanf("%s", username); key.data = &userid; key.size = sizeof(int); data.data = username; data.size = strlen(username) + 1; ret = ptr_db->put(ptr_db, NULL, &key, &data, DB_NOOVERWRITE); if (DB_KEYEXIST == ret) { printf("DB Key already exist!\n"); } else { printf("Add Record Succeeded!\n"); } break; case 2: printf("Please input the key:"); scanf("%d", &userid); key.data = &userid; key.size = sizeof(int); data.data = username; data.ulen = 100; data.flags = DB_DBT_USERMEM; ret = ptr_db->get(ptr_db, NULL, &key, &data, 0); if (DB_NOTFOUND == ret) { printf("User ID %d Not Exist!\n", userid); } else { printf("User ID:%d\nUser Name:%s\n", userid, username); } break; case 3: printf("Please input the key:"); scanf("%d", &userid); key.data = &userid; key.size = sizeof(int); ptr_db->del(ptr_db, NULL, &key, 0); printf("You have delete user %d succeeded\n", userid); break; case 4: goto finish; break; default: printf("Unkown selection.\n"); break; } } //clean finish: if (NULL != fptr_db_err_log) { fclose(fptr_db_err_log); } if (NULL != fptr_db_env_err_log) { fclose(fptr_db_env_err_log); } if (NULL != ptr_db) { ptr_db->close(ptr_db, 0); } if (NULL != ptr_db_env) { ptr_db_env->close(ptr_db_env, 0); } return ret; }