Berkeley DB 简单综合范例

 在参考前一篇代码基础上,有所添加。在Red Hat Linux 8.0平台,测试通过。

在第一次运行的时候,要在应用程序所在的目录新建立一个文件夹“dbtestENV”,存放环境用;其次,要把注释掉的代码还原,因为得向空的数据库里添加数据。之后可以注释掉。

程序大意:在一个环境(dbenv)中打开两个数据库(dbp1, dbp2),建立指向两个数据库的游标(cur1, cur2),dbp1是在一个事务(txn)中运行。对两个数据库的动作都是添加数据,查找显示数据。

#include <db.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

void print_error(int ret)
{
 if(ret != 0)
  printf("ERROR: %s/n",db_strerror(ret));
}

void init_DBT(DBT * key, DBT * data)
{
 memset(key, 0, sizeof(DBT));
 memset(data, 0, sizeof(DBT));
}

int main(void)
{
 DB *dbp1, *dbp2;          
 DBT key, data;
 DB_ENV *dbenv;
 DBC *cur1, *cur2;
 DB_TXN *txn = NULL;
 u_int32_t flags; 
 int ret;

 char *fruit = "apple";
/* int number = 15;
 char *orange = "orange";
 int number2 = 10;
*/
 typedef struct customer
 {
  int  c_id;
  char name[10];
  char address[20];
  int  age;
 } CUSTOMER;
 CUSTOMER cust;

 cust.c_id = 1;
 strncpy(cust.name, "javer", 9);
 strncpy(cust.address, "chengdu", 19);
 cust.age = 32;
 

 ret = db_env_create(&dbenv, 0);
 print_error(ret);

 ret = dbenv->open(dbenv, "./dbtestENV", DB_CREATE|DB_INIT_LOG|DB_INIT_LOCK| DB_INIT_MPOOL|DB_INIT_TXN, 0);
 print_error(ret);

 ret = db_create(&dbp1, dbenv, 0);
 print_error(ret);

 ret = dbenv->txn_begin(dbenv, NULL, &txn, 0);
 print_error(ret);

 flags = DB_CREATE;
 ret = dbp1->open(dbp1, txn, "single.db", NULL, DB_BTREE, flags, 0);
 print_error(ret);
 
 ret = dbp1->cursor(dbp1, txn, &cur1, 0);
 print_error(ret);
 
 init_DBT(&key, &data);
 key.data = fruit;
 key.size = strlen(fruit) + 1;
/* data.data = &number;
 data.size = sizeof(int);
 ret = dbp1->put(dbp1, txn, &key, &data, DB_NOOVERWRITE);
 print_error(ret);
 init_DBT(&key, &data);
 key.data = orange;
 key.size = strlen(orange) + 1;
 data.data = &number2;
 data.size = sizeof(int);
 ret = dbp1->put(dbp1, txn, &key, &data, DB_NOOVERWRITE);
 print_error(ret);
*/
 while((ret = cur1->c_get(cur1, &key, &data, DB_NEXT)) == 0)
 {
  printf("The fruit = %s, The number = %d/n", (char *)(key.data), *(int*)(data.data));
 }

 ret = db_create(&dbp2, dbenv, 0);
 print_error(ret);

 flags = DB_CREATE;   

 ret = dbp2->open(dbp2, NULL, "complex.db",NULL, DB_HASH, flags, 0);
 print_error(ret);

 ret = dbp2->cursor(dbp2, NULL, &cur2, 0);
 print_error(ret);
 
 init_DBT(&key, &data);
 key.size = sizeof(int);
 key.data = &(cust.c_id);
 data.data = &cust;
 data.ulen = sizeof(CUSTOMER);
 data.flags = DB_DBT_USERMEM;

 //ret = dbp2->put(dbp2, NULL, &key, &data,DB_NOOVERWRITE);
 //print_error(ret);

 while((ret = cur2->c_get(cur2, &key, &data, DB_NEXT)) == 0)
 {
  printf("c_id = %d name = %s address = %s age = %d/n",
   cust.c_id, cust.name, cust.address, cust.age);
 }

 ret = cur1->c_close(cur1);
 print_error(ret);
 ret = cur2->c_close(cur2);
 print_error(ret);
 ret = txn->commit(txn, 0);
 print_error(ret);
 if(dbp1 != NULL)
  dbp1->close(dbp1, 0);
 if(dbp2 != NULL)
  dbp2->close(dbp2, 0);
     dbenv->close(dbenv, 0);
 return 0;
}


本文来自: (www.91linux.com) 详细出处参考:http://www.91linux.com/html/article/database/20071202/8696.html

 

 

BDB读、写、删除、更新事物的简单例子来源: 作者: 时间:2007-12-02 Tag: 点击: 35 #include <db.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

int fun_main(int ,int ,int);
int  generate_db_data(); //生成数据库
int output_db_data();  //输出数据库
int operate_db_data();  //对数据库的操作选择
int DB_Insert();     //插入
int DB_Delete();   //删除
int DB_Modify();    //修改
int DB_Seach();   //查找

void print_error(int ret)
{
 if(ret != 0)
  printf("ERROR: %s/n",db_strerror(ret));
}

void init_DBT(DBT * key, DBT * data)
{
 memset(key, 0, sizeof(DBT));
 memset(data, 0, sizeof(DBT));
}


int main(int argc, char *argv[])
{
 int ret;
 int generate, output, process;
 
 ret = 0; generate = 0; output = 0; process = 0;
 if(argc != 2)
 {
  printf("argv error! Please run the program with parameter./n");
  printf("Sample : ./fun -g/n");
  exit(ret);
 }
 while(*argv[1] != '/0')
 {
  if(*argv[1] == '-'){
   argv[1]++;
   continue;
  }
  else if(*argv[1] == 'g')
   generate = 1;
  else if(*argv[1] == 'o')
   output = 1;
  else if(*argv[1] == 'p')
   process = 1;
  argv[1]++;
 }
 ret = fun_main(generate, output, process);
 
 exit(ret);
}

int fun_main(int generate, int output, int process)
{
 int ret = 0;
 if(generate == 1&& process != 1){
 
     //DBデータの生成
     ret = generate_db_data();
    
 }
 if(output == 1&& process != 1){
 
  //DBデータの出力
  ret = output_db_data();
 }
 if(process == 1)
 {
  //DBデータの操作
  ret = operate_db_data();
 }
 if(generate == 0 && output == 0 && process == 0)
 {
  printf("parameter input error, please run again!/n");
  printf("Sample : ./fun -o/n");
 }
    return ret;
}
int generate_db_data()
{
 int ret = 0,i;
 DB *dbp1;          
 DBT key, data;
 DB_ENV *dbenv;
 u_int32_t flags; 

//生成10各元素的数据库。

 char *stu_name[10] = {"apple", "orange","banana","strawberry","watermelon","grapes","pineapple","plum","pear","peach"};   int stu_id[10];
 for(i=0; i<10; i++)
  stu_id[i] = i+1;

 ret = db_env_create(&dbenv, 0);
 print_error(ret);

 ret = dbenv->open(dbenv, "./dbtestENV", DB_CREATE|DB_INIT_LOG|DB_INIT_LOCK| DB_INIT_MPOOL|DB_INIT_TXN, 0);
 print_error(ret);

 ret = db_create(&dbp1, dbenv, 0);
 print_error(ret);

 flags = DB_CREATE;
 ret = dbp1->open(dbp1, NULL, "single.db", NULL, DB_BTREE, flags, 0);
 print_error(ret);
 
 for(i=0; i<10; i++){
  init_DBT(&key, &data);
  key.data = stu_name[i];
  key.size = strlen(stu_name[i]) + 1;
  data.data = &stu_id[i];
  data.size = sizeof(int);
  ret = dbp1->put(dbp1, NULL, &key, &data, DB_NOOVERWRITE);
  print_error(ret);
 }
 
 if(dbp1 != NULL)
  dbp1->close(dbp1, 0);
     dbenv->close(dbenv, 0);
 return 0;
}
int output_db_data()
{
 int ret = 0;
 DB *dbp1;          
 DBT key, data;
 DB_ENV *dbenv;
 DBC *cur1;
 DB_TXN *txn1 = NULL;
 u_int32_t flags; 

 ret = db_env_create(&dbenv, 0);
 print_error(ret);

 ret = dbenv->open(dbenv, "./dbtestENV", DB_CREATE|DB_INIT_LOG|DB_INIT_LOCK| DB_INIT_MPOOL|DB_INIT_TXN, 0);
 print_error(ret);

 ret = db_create(&dbp1, dbenv, 0);
 print_error(ret);

 ret = dbenv->txn_begin(dbenv, NULL, &txn1, 0);
 print_error(ret);

 flags = DB_CREATE;
 ret = dbp1->open(dbp1, txn1, "single.db", NULL, DB_BTREE, flags, 0);
 print_error(ret);
 
 ret = dbp1->cursor(dbp1, txn1, &cur1, 0);
 print_error(ret);
 
 init_DBT(&key, &data);
 while((ret = cur1->c_get(cur1, &key, &data, DB_NEXT)) == 0)
 {
  printf("The fruit = %s,/t The number = %d/n", (char *)(key.data), *(int*)(data.data));
 }

 ret = cur1->c_close(cur1);
 print_error(ret);
 ret = txn1->commit(txn1, 0);
 print_error(ret);

 if(dbp1 != NULL)
  dbp1->close(dbp1, 0);
     dbenv->close(dbenv, 0);
 return 0;
}
int operate_db_data()
{
 int ret = 0,sel = 0;
 printf("What do you want to do :/n1.Insert/n2.Delete/n3.Modify/n4.Search/n");
 scanf("%d",&sel);
 switch(sel)
 {
  case 1:
   DB_Insert();
   break;
  case 2:
   DB_Delete();
   break;
  case 3:
   DB_Modify();
   break;
  case 4:
   DB_Seach();
   break;
  default:
   printf("please select from 1-4! /n");
 }
 return ret;
}
int DB_Insert()
{
 int ret = 0;
 DB *dbp1;          
 DBT key, data;
 DB_ENV *dbenv;
 u_int32_t flags; 

 char name[20] = {0};
 int number;
 printf("Please input the data and value :");
 scanf("%s%d",name,&number);
 printf("You insert data : The fruit =/"%s/", the number = /"%d/"/n",name,number);

 ret = db_env_create(&dbenv, 0);
 print_error(ret);

 ret = dbenv->open(dbenv, "./dbtestENV", DB_CREATE|DB_INIT_LOG|DB_INIT_LOCK| DB_INIT_MPOOL|DB_INIT_TXN, 0);
 print_error(ret);

 ret = db_create(&dbp1, dbenv, 0);
 print_error(ret);

 flags = DB_CREATE;
 ret = dbp1->open(dbp1, NULL, "single.db", NULL, DB_BTREE, flags, 0);
 print_error(ret);
 init_DBT(&key, &data);
 key.data = name;
 key.size = strlen(name) + 1;
 data.data = &number;
 data.size = sizeof(int);
 ret = dbp1->put(dbp1, NULL, &key, &data, DB_NOOVERWRITE);
 print_error(ret);

 if(ret == 0)
  printf("INSERT SUCCESS!/n");
 else
  printf("INSERT FAILED!/n");
 if(dbp1 != NULL)
  dbp1->close(dbp1, 0);
     dbenv->close(dbenv, 0);
 return ret;
}
int DB_Delete()
{
 int ret=0;
 char name[20];
 DB *dbp1;          
 DBT key, data;
 DB_ENV *dbenv;
 u_int32_t flags; 

 printf("Which data do you want to delete: ");
 scanf("%s",name);
 printf("You want to delete the data is :/"%s/"/n",name);
 
 ret = db_env_create(&dbenv, 0);
 print_error(ret);

 ret = dbenv->open(dbenv, "./dbtestENV", DB_CREATE|DB_INIT_LOG|DB_INIT_LOCK| DB_INIT_MPOOL|DB_INIT_TXN, 0);
 print_error(ret);

 ret = db_create(&dbp1, dbenv, 0);
 print_error(ret);

 flags = DB_CREATE;
 ret = dbp1->open(dbp1, NULL, "single.db", NULL, DB_BTREE, flags, 0);
 print_error(ret);
 init_DBT(&key, &data);
 key.data = name;
 key.size = strlen(name) + 1;
 ret = dbp1->del(dbp1, NULL, &key, 0);
 print_error(ret);

 if(ret == 0)
  printf("DELETE SUCCESS!/n");
 else
  printf("DELETE FAILED!/n");
 if(dbp1 != NULL)
  dbp1->close(dbp1, 0);
     dbenv->close(dbenv, 0);

 return ret;
}
int DB_Modify()
{
 int ret = 0;
 DB *dbp1;          
 DBT key, data;
 DB_ENV *dbenv;
 DBC *cur1;
 DB_TXN *txn1 = NULL;
 u_int32_t flags; 
 
 char name_old[20], name_new[20];
 int number;
 printf("Which data do you want to modify(key name): ");
 scanf("%s",name_old);
 printf("The new data is :");
 scanf("%s%d",name_new, &number);

 ret = db_env_create(&dbenv, 0);
 print_error(ret);

 ret = dbenv->open(dbenv, "./dbtestENV", DB_CREATE|DB_INIT_LOG|DB_INIT_LOCK| DB_INIT_MPOOL|DB_INIT_TXN, 0);
 print_error(ret);

 ret = db_create(&dbp1, dbenv, 0);
 print_error(ret);

 ret = dbenv->txn_begin(dbenv, NULL, &txn1, 0);
 print_error(ret);

 flags = DB_CREATE;
 ret = dbp1->open(dbp1, txn1, "single.db", NULL, DB_BTREE, flags, 0);
 print_error(ret);
 
 ret = dbp1->cursor(dbp1, txn1, &cur1, 0);
 print_error(ret);
 
 init_DBT(&key, &data);
 key.data = name_old;
 key.size = strlen(name_old) + 1;

 while((ret = cur1->c_get(cur1, &key, &data, DB_SET)) == 0)
 {
  printf("The data you want to modify is: The fruit = %s,/t The number = %d/n", (char *)(key.data), *(int*)(data.data));
  ret = cur1->c_del(cur1, 0);
  break;
 }
  init_DBT(&key, &data);
  key.data = name_new;
  key.size = strlen(name_new) + 1;
  data.data = &number;
  data.size = sizeof(int);
  ret = dbp1->put(dbp1, txn1, &key, &data, DB_NOOVERWRITE);

 if(ret == 0)
  printf("MODIFY SUCCESS!/n");
 else
  printf("MODIFY FAILED!/n");

 ret = cur1->c_close(cur1);
 print_error(ret);
 ret = txn1->commit(txn1, 0);
 print_error(ret);
 if(dbp1 != NULL)
  dbp1->close(dbp1, 0);
     dbenv->close(dbenv, 0);
 return 0;
}
int DB_Seach()
{
 int ret = 0;
 DB *dbp1;          
 DBT key, data;
 DB_ENV *dbenv;
 DBC *cur1;
 DB_TXN *txn1 = NULL;
 u_int32_t flags; 
 
 char name[20];
 printf("Which data do you want to search: ");
 scanf("%s",name);
 printf("You want to search the data is :/"%s/"/n",name);

 ret = db_env_create(&dbenv, 0);
 print_error(ret);

 ret = dbenv->open(dbenv, "./dbtestENV", DB_CREATE|DB_INIT_LOG|DB_INIT_LOCK| DB_INIT_MPOOL|DB_INIT_TXN, 0);
 print_error(ret);

 ret = db_create(&dbp1, dbenv, 0);
 print_error(ret);

 ret = dbenv->txn_begin(dbenv, NULL, &txn1, 0);
 print_error(ret);

 flags = DB_CREATE;
 ret = dbp1->open(dbp1, txn1, "single.db", NULL, DB_BTREE, flags, 0);
 print_error(ret);
 
 ret = dbp1->cursor(dbp1, txn1, &cur1, 0);
 print_error(ret);
 
 init_DBT(&key, &data);
 key.data = name;
 key.size = strlen(name) + 1;

 while((ret = cur1->c_get(cur1, &key, &data, DB_SET)) == 0)
 {
  printf("The fruit = %s,/t The number = %d/n", (char *)(key.data), *(int*)(data.data));
  break;
 }
 if(ret != 0)
  printf("The data which you want cannot found./n");
 
 ret = cur1->c_close(cur1);
 print_error(ret);
 ret = txn1->commit(txn1, 0);
 print_error(ret);
 if(dbp1 != NULL)
  dbp1->close(dbp1, 0);
     dbenv->close(dbenv, 0);
 return 0;

}


本文来自: (www.91linux.com) 详细出处参考:http://www.91linux.com/html/article/database/20071202/8695.html

你可能感兴趣的:(apple,数据库,null,delete,insert,output)