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 );