Library Version 12.1.6.2

Putting Records Using Cursors
Prev | Chapter 9. Using Cursors | Next

## Putting Records Using Cursors

You can use cursors to put records into the database. DB's behavior when putting records into the database differs depending on the flags that you use when writing the record, on the access method that you are using, and on whether your database supports sorted duplicates.

Note that when putting records to the database using a cursor, the cursor is positioned at the record you inserted.

- `Cursor.putNoDupData()`

  If the provided key already exists in the database, then this method returns `OperationStatus.KEYEXIST`.

  If the key does not exist, then the order that the record is put into the database is determined by the insertion order in use by the database. If a comparison function has been provided to the database, the record is inserted in its sorted location. Otherwise (assuming BTree), lexicographical sorting is used, with shorter items collating before longer items.

  This flag can only be used for the BTree and Hash access methods, and only if the database has been configured to support sorted duplicate data items (`DB_DUPSORT` was specified at database creation time).

  This flag cannot be used with the Queue or Recno access methods.

  For more information on duplicate records, see Allowing Duplicate Records.

- `Cursor.putNoOverwrite()`

  If the provided key already exists in the database, then this method returns .

  If the key does not exist, then the order that the record is put into the database is determined by the BTree (key) comparator in use by the database.

- `Cursor.putKeyFirst()`

  For databases that do not support duplicates, this method behaves exactly the same as if a default insertion was performed. If the database supports duplicate records, and a duplicate sort function has been specified, the inserted data item is added in its sorted location. If the key already exists in the database and no duplicate sort function has been specified, the inserted data item is added as the first of the data items for that key.

- `Cursor.putKeyLast()`

  Behaves exactly as if `Cursor.putKeyFirst()` was used, except that if the key already exists in the database and no duplicate sort function has been specified, the inserted data item is added as the last of the data items for that key.

For example:

    package db.GettingStarted;

    import com.sleepycat.db.Cursor;
    import com.sleepycat.db.Database;
    import com.sleepycat.db.DatabaseEntry;
    import com.sleepycat.db.OperationStatus;

    ...

    // Create the data to put into the database
    String key1str = "My first string";
    String data1str = "My first data";
    String key2str = "My second string";
    String data2str = "My second data";
    String data3str = "My third data";

    Cursor cursor = null;
    Database myDatabase = null;
    try {
        ...
        // Database open omitted for brevity
        ...

        DatabaseEntry key1 = new DatabaseEntry(key1str.getBytes("UTF-8"));
        DatabaseEntry data1 = new DatabaseEntry(data1str.getBytes("UTF-8"));
        DatabaseEntry key2 = new DatabaseEntry(key2str.getBytes("UTF-8"));
        DatabaseEntry data2 = new DatabaseEntry(data2str.getBytes("UTF-8"));
        DatabaseEntry data3 = new DatabaseEntry(data3str.getBytes("UTF-8"));

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

        // Assuming an empty database.

        OperationStatus retVal = cursor.put(key1, data1); // SUCCESS
        retVal = cursor.put(key2, data2); // SUCCESS
        retVal = cursor.put(key2, data3); // SUCCESS if dups allowed,
                                          // KEYEXIST if not.

    } catch (Exception e) {
        // Exception handling goes here
    } finally {
       // Make sure to close the cursor
       cursor.close();
    }

Prev | Up | Next
Getting Records Using the Cursor | Home | Deleting Records Using Cursors