Berkely DB C++演示代码(写入重复记录)

class customer
{
public:
 customer() { }
 ~customer() { }

public:
 int  c_id;
 char name[10];
 char address[20];
 int  age;

 std::string sztest;
 
};

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

void CTestDBDlg::OnOK()
{
 DBT key, data;
 u_int32_t flags;
 int ret;

 customer cust;
 cust.c_id = 1;
 strcpy(cust.name, "wu");
 strcpy(cust.address, "shanghai");
 cust.age = 25;
 cust.sztest = "23333";

 DB* mydb = NULL;
 ret = db_create(&mydb, NULL, 0);
    mydb->set_flags(mydb, DB_DUP);
 ret = mydb->open( mydb, NULL, "mydb2222.db", NULL, DB_BTREE, DB_CREATE, 0);
 init_DBT(&key, &data);

 // 第一条记录
 key.size = sizeof(int);
 key.data = &(cust.c_id);
 data.size = sizeof(customer);
 data.data = &cust;
 ret = mydb->put(mydb, NULL, &key, &data,0);

    // 第二条记录
 init_DBT(&key, &data);
 strcpy( cust.name, "wuhuiran" );
 key.size = sizeof(int);
 key.data = &(cust.c_id);
 data.size = sizeof(customer);
 data.data = &cust;
 ret = mydb->put(mydb, NULL, &key, &data,0);

//  init_DBT(&key, &data);
//  other ot;
//  ot.id = 10;
//  strcpy(ot.name, "hhh");
//
//  key.size = sizeof(int);
//  key.data = &(ot.id);
//  data.size = sizeof(other);
//  data.data = &ot;
//  ret = mydb->put(mydb, NULL, &key, &data, 0);

 memset(&cust, 0, sizeof(customer));
//  memset(&ot, 0, sizeof(other));

 int get_key = 1;
 // 用游标读取重复的记录
 DBC          *cursorp_;
 mydb->cursor( mydb, NULL, &cursorp_, 0 );

 init_DBT(&key, &data);
 key.size = sizeof(int);
 key.data = &get_key;
 data.data = &cust;
 data.ulen = sizeof(customer);
 data.flags = DB_DBT_USERMEM;
 while( cursorp_->c_get( cursorp_, &key, &data, DB_NEXT ) != DB_NOTFOUND )
 {
  CString strMsg;
  strMsg.Format("%d_%s", cust.c_id, cust.name);
  AfxMessageBox(strMsg);

 }
 if ( cursorp_ != NULL )
 {
  cursorp_->c_close( cursorp_ );
  cursorp_ = NULL;
 }
 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值