|
|
To delete a record using a cursor, simply position the cursor to the record that you want to delete and then call DBC->c_del().
For example:
#include <db.h> #include <string.h> ... DB *dbp; DBC *cursorp; DBT key, data; char *key1str = "My first string"; int ret; /* Set up our DBTs */ key.data = key1str; key.size = strlen(key1str) + 1; /* Database open omitted */ /* Get the cursor */ dbp->cursor(dbp, NULL, &cursorp, 0); /* Initialize our DBTs. */ memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); /* Iterate over the database, deleting each record in turn. */ while ((ret = cursorp->c_get(cursorp, &key, &data, DB_SET)) == 0) { cursorp->c_del(cursorp, 0); } /* Cursors must be closed */ if (cursorp != NULL) cursorp->c_close(cursorp); if (dbp != NULL) dbp->close(dbp, 0);