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

    技术2022-05-11  23

    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)