Bdb btree replacement record

Source: Internet
Author: User

Replacing repeated record data in a btree that supports sorted duplicates will cause replacement failure due to sorting rules.

ArticleFor this type of btree, if you want to replace the data of Repeated Records, you must first Delete the data and then put the new record.

ExampleCode:

# Include <iostream> # include <db_cxx.h> # include "print. HH "# define Database" duplicated. DB "int main () {dB my_database (null, 0); DBC * cursorp; int nret = 0; try {// open databasemy_database.open (null, database, null, db_btree, db_create, 0); // open cursormy_database.cursor (null, & cursorp, 0); // print all recordsrecords_print (cursorp, "before:"); char * key1str = "Alabama "; char * data1str = "Florence"; char * Replacement_data = "replace me"; // set up on dbtdbt key (key1str, strlen (key1str) + 1); DBT data; // position the cursor for sort duplicated databasenret = cursorp-> get (& Key, & Data, db_set); If (nret = 0) {data. set_data (replacement_data); data. set_size (strlen (replacement_data) + 1); cursorp-> Del (0); // Delete the record to be replaced nret = my_database.put (null, & Key, & Data, 0 ); // Insert a new record if (nret! = 0) {STD: cout <"put failed! "<STD: Endl ;}// print all recordsrecords_print (cursorp," After: ") ;}catch (dbexception & E) {STD :: cout <"DB exception:" <E. what () <STD: Endl;} catch (STD: exception & E) {STD: cout <"STD: exception:" <E. what () <STD: Endl;} If (cursorp! = NULL) cursorp-> close (); my_database.close (0); Return 0 ;}

Execution result:
Before:
Alabama Athens
Alabama Florence
Alaska Anchorage
Alaska Fairbanks
Arizona Florence
After:
Alabama Florence
Alabama replace me
Alaska Anchorage
Alaska Fairbanks
Arizona Florence
Press any key to continue

Attach print. HH

 
// Use cursor to print all records # define records_print (cursorp, optstr) \ do {\ STD: cout <optstr <STD: Endl; \ DBC * cursorp_d; \ cursorp-> DUP (& cursorp_d, 0); \ DBT key, data; \ while (nret = cursorp_d-> get (& Key, & Data, db_next ))! = Db_notfound) {\ STD: cout <(char *) Key. get_data () <"<(char *) data. get_data () <STD: Endl ;\}\} while (0 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.