Using Cursors with Secondary Databases

Just like cursors on a primary database, you can use cursors on secondary databases to iterate over the records in a secondary database. Like cursors used with primary databases, you can also use cursors with secondary databases to search for specific records in a database, to seek to the first or last record in the database, to get the next duplicate record, and so forth. For a complete description on cursors and their capabilities, see Using Cursors.

However, when you use cursors with secondary databases:

For example, suppose you are using the databases, classes, and key extractors described in Implementing Key Extractors . Then the following searches for a person's name in the secondary database, and deletes all secondary and primary records that use that name.

#include <db_cxx.h>
                                                                                                                                     
...
                                                                                                                                     
Db my_database(NULL, 0);
Db my_index(NULL, 0);


// Get a cursor on the secondary database
Dbc *cursorp;
my_index.cursor(NULL, &cursorp, 0);


// Name to delete
char *search_name = "John Doe"; 

// Instantiate Dbts as normal
Dbt key(search_name, strlen(search_name) + 1);
Dbt data;

 
// Position the cursor
while (cursorp->get(&key, &data, DB_SET) == 0)
    cursorp->del(0);