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.h>
#include <string.h>

...
                                                                                                                                     
DB *sdbp;          /* Secondary DB handle */
DBC *cursorp;      /* Cursor */
DBT key, data;     /* DBTs used for the delete */
char *search_name = "John Doe"; /* Name to delete */
                                                                                                                                     
/* Primary and secondary database opens omitted for brevity. */

/* Get a cursor on the secondary database */
sdbp->cursor(sdbp, NULL, &cursorp, 0);

/*
 * Zero out the DBT before using it.
 */
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));

key.data = search_name;
key.size = strlen(search_name) + 1;

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