Deleting Records Using Cursors

To delete a record using a cursor, simply position the cursor to the record that you want to delete and then call Cursor.delete(). Note that after deleting a record, the value of Cursor.getCurrent() is unchanged until such a time as the cursor is moved again. Also, if you call Cursor.delete() two or more times in a row without repositioning the cursor, then all subsequent deletes result in a return value of OperationStatus.KEYEMPTY.

For example:

package je.gettingStarted;


Cursor cursor = null;
try {
    // Database and environment open omitted for brevity
    // Create DatabaseEntry objects
    // searchKey is some String.
    DatabaseEntry theKey = new DatabaseEntry(searchKey.getBytes("UTF-8"));
    DatabaseEntry theData = new DatabaseEntry();

    // Open a cursor using a database handle
    cursor = myDatabase.openCursor(null, null);

    // Position the cursor. Ignoring the return value for clarity
    OperationStatus retVal = cursor.getSearchKey(theKey, theData, 
    // Count the number of records using the given key. If there is only
    // one, delete that record.
    if (cursor.count() == 1) {
            System.out.println("Deleting " + 
                               new String(theKey.getData(), "UTF-8") +
                               "|" + 
                               new String(theData.getData(), "UTF-8"));
} catch (Exception e) {
    // Exception handling goes here
} finally {
   // Make sure to close the cursor