Description: Berkeley DB: An embedded database programmatic toolkit.
Keywords: embedded,database,programmatic,toolkit,b+tree,btr ee,hash,hashing,transaction,transactions,locking,logging,access method,access me thods,java,C,C++

### Berkeley DB Reference Guide: Simple Tutorial

# Removing elements from a database

The simplest way to remove elements from a database is the DB->del interface. This interface is accessed through a function pointer that is an element of the database handle returned by db_open.

The DB->del interface takes four of the same five arguments that the DB->get and DB->put interfaces take. The difference is that there is no need to specify a data item, as the delete operation is only interested in the key that you want to remove.

db
  The database handle returned by db_open.

txnid
  A transaction ID. In our simple case, we aren't expecting to recover the database after application or system crash, so we aren't using transactions, and will leave this argument unspecified.

key
  The key item for the key/data pair that we want to retrieve from the database.

flags
  Optional flags modifying the underlying behavior of the DB->del interface. There are currently no available flags for this interface, so the flags argument should always be set to 0.

Here's what the code to call DB->del looks like:

    #include <sys/types.h>
    #include <stdio.h>
    #include <db.h>

    #define	DATABASE	"access.db"

    int
    main()
    {
    	extern int errno;
    	DB *dbp;
    	DBT key, data;

    	if ((errno = db_open(DATABASE,
    	    DB_BTREE, DB_CREATE, 0664, NULL, NULL, &dbp)) != 0) {
    		fprintf(stderr, "db: %s: %s\n", DATABASE, strerror(errno));
    		exit (1);
    	}

    	memset(&key, 0, sizeof(key));
    	memset(&data, 0, sizeof(data));
    	key.data = "fruit";
    	key.size = sizeof("fruit");
    	data.data = "apple";
    	data.size = sizeof("apple");

    	switch (errno = dbp->put(dbp, NULL, &key, &data, 0)) {
    	case 0:
    		printf("db: %s: key stored.\n", (char *)key.data);
    		break;
    	default:
    		fprintf(stderr, "db: put: %s\n", strerror(errno));
    		exit (1);
    	}

    	switch (errno = dbp->get(dbp, NULL, &key, &data, 0)) {
    	case 0:
    		printf("db: %s: key retrieved: data was %s.\n",
    		    (char *)key.data, (char *)data.data);
    		break;
    	case DB_NOTFOUND:
    		printf("db: %s: key not found.\n", (char *)key.data);
    		break;
    	default:
    		fprintf(stderr, "db: get: %s\n", strerror(errno));
    		exit (1);
    	}

    	switch (errno = dbp->del(dbp, NULL, &key, 0)) {
    	case 0:
    		printf("db: %s: key was deleted.\n", (char *)key.data);
    		break;
    	default:
    		fprintf(stderr, "db: del: %s\n", strerror(errno));
    		exit (1);
    	}

    	switch (errno = dbp->get(dbp, NULL, &key, &data, 0)) {
    	case 0:
    		printf("db: %s: key retrieved: data was %s.\n",
    		    (char *)key.data, (char *)data.data);
    		break;
    	case DB_NOTFOUND:
    		printf("db: %s: key not found.\n", (char *)key.data);
    		break;
    	default:
    		fprintf(stderr, "db: get: %s\n", strerror(errno));
    		exit (1);
    	}