Getting Records Using the Cursor
Prev | Chapter 4. Using Cursors | Next

## Getting Records Using the Cursor

To iterate over database records, from the first record to the last, simply open the cursor and then use the `Dbc::get()` method. Note that you need to supply the `DB_NEXT` flag to this method. For example:

    #include <db_cxx.h>

    ...

    Db my_database(NULL, 0);
    Dbc *cursorp;

    try {
        // Database open omitted for clarity

        // Get a cursor
        my_database.cursor(NULL, &cursorp, 0);

        Dbt key, data;
        int ret;

        // Iterate over the database, retrieving each record in turn.
        while ((ret = cursorp->get(&key, &data, DB_NEXT)) == 0) {
            // Do interesting things with the Dbts here.
        }
        if (ret != DB_NOTFOUND) {
            // ret should be DB_NOTFOUND upon exiting the loop.
            // Dbc::get() will by default throw an exception if any
            // significant errors occur, so by default this if block
            // can never be reached.
        }
    } catch(DbException &e) {
            my_database.err(e.get_errno(), "Error!");
    } catch(std::exception &e) {
            my_database.errx("Error! %s", e.what());
    }

    // Cursors must be closed
    if (cursorp != NULL)
        cursorp->close();

    my_database.close(0);

To iterate over the database from the last record to the first, use `DB_PREV` instead of `DB_NEXT`:

    #include <db_cxx.h>

    ...

    Db my_database(NULL, 0);
    Dbc *cursorp;

    try {
        // Database open omitted for clarity

        // Get a cursor
        my_database.cursor(NULL, &cursorp, 0);

        Dbt key, data;
        int ret;
        // Iterate over the database, retrieving each record in turn.
        while ((ret = cursorp->get(&key, &data, DB_PREV)) == 0) {
            // Do interesting things with the Dbts here.
        }
        if (ret != DB_NOTFOUND) {
            // ret should be DB_NOTFOUND upon exiting the loop.
            // Dbc::get() will by default throw an exception if any
            // significant errors occur, so by default this if block
            // can never be reached.
        }
    } catch(DbException &e) {
            my_database.err(e.get_errno(), "Error!");
    } catch(std::exception &e) {
            my_database.errx("Error! %s", e.what());
    }

    // Cursors must be closed
    if (cursorp != NULL)
        cursorp->close();

    my_database.close(0);

### Searching for Records

You can use cursors to search for database records. You can search based on just a key, or you can search based on both the key and the data. You can also perform partial matches if your database supports sorted duplicate sets. In all cases, the key and data parameters of these methods are filled with the key and data values of the database record to which the cursor is positioned as a result of the search.

Also, if the search fails, then cursor's state is left unchanged and `DB_NOTFOUND` is returned.

To use a cursor to search for a record, use Dbt::get()``. When you use this method, you can provide the following flags:

### Note

Notice in the following list that the cursor flags use the keyword `SET` when the cursor examines just the key portion of the records (in this case, the cursor is set to the record whose key matches the value provided to the cursor). Moreover, when the cursor uses the keyword `GET`, then the cursor is positioned to both the key and the data values provided to the cursor.

Regardless of the keyword you use to get a record with a cursor, the cursor's key and data `Dbt`s are filled with the data retrieved from the record to which the cursor is positioned.

- `DB_SET`

  Moves the cursor to the first record in the database with the specified key.

- `DB_SET_RANGE`

  Identical to `DB_SET` `Cursor.getSearchKey()` unless you are using the BTree access. In this case, the cursor moves to the first record in the database whose key is greater than or equal to the specified key. This comparison is determined by the comparison function that you provide for the database. If no comparison function is provided, then the default lexicographical sorting is used.

  For example, suppose you have database records that use the following Strings as keys:

      Alabama
      Alaska
      Arizona

  Then providing a search key of `Alaska` moves the cursor to the second key noted above. Providing a key of `Al` moves the cursor to the first key (`Alabama`), providing a search key of `Alas` moves the cursor to the second key (`Alaska`), and providing a key of `Ar` moves the cursor to the last key (`Arizona`).

- `DB_GET_BOTH`

  Moves the cursor to the first record in the database that uses the specified key and data.

- `DB_GET_BOTH_RANGE`

  Moves the cursor to the first record in the database whose key is greater than or equal to the specified key. If the database supports duplicate records, then on matching the key, the cursor is moved to the duplicate record with the smallest data that is greater than or equal to the specified data.

  For example, suppose your database uses BTree and it has database records that use the following key/data pairs:

      Alabama/Athens
      Alabama/Florence
      Alaska/Anchorage
      Alaska/Fairbanks
      Arizona/Avondale
      Arizona/Florence

  then providing:

  a search key of ... | and a search data of ... | moves the cursor to ...
  --- | --- | ---
  Al | Fl | Alabama/Florence
  Ar | Fl | Arizona/Florence
  Al | Fa | Alaska/Fairbanks
  Al | A | Alabama/Athens

For example, assuming a database containing sorted duplicate records of U.S. States/U.S Cities key/data pairs (both as Strings), then the following code fragment can be used to position the cursor to any record in the database and print its key/data values:

    #include <db_cxx.h>
    #include <string.h>

    ...

    Db my_database(NULL, 0);
    Dbc *cursorp;

    try {
        // database open omitted for clarity

        // Get a cursor
        my_database.cursor(NULL, &cursorp, 0);

        // Search criteria
        char *search_key = "Al";
        char *search_data = "Fa";

        // Set up our DBTs
        Dbt key(search_key, strlen(search_key) + 1);
        Dbt data(search_data, strlen(search_data) + 1);

        // Position the cursor to the first record in the database whose
        // key and data begin with the correct strings.
        int ret = cursorp->get(&key, &data, DB_GET_BOTH_RANGE);
        if (!ret) {
            // Do something with the data
        }
    } catch(DbException &e) {
            my_database.err(e.get_errno(), "Error!");
    } catch(std::exception &e) {
            my_database.errx("Error! %s", e.what());
    }

    // Close the cursor
    if (cursorp != NULL)
        cursorp->close();

    // Close the database
    my_database.close(0);

### Working with Duplicate Records

A record is a duplicate of another record if the two records share the same key. For duplicate records, only the data portion of the record is unique.

Duplicate records are supported only for the BTree or Hash access methods. For information on configuring your database to use duplicate records, see Allowing Duplicate Records.

If your database supports duplicate records, then it can potentially contain multiple records that share the same key. By default, normal database get operations will only return the first such record in a set of duplicate records. Typically, subsequent duplicate records are accessed using a cursor. The following `Dbc::get()` flags are interesting when working with databases that support duplicate records:

- `DB_NEXT`, `DB_PREV`

  Shows the next/previous record in the database, regardless of whether it is a duplicate of the current record. For an example of using these methods, see Getting Records Using the Cursor.

- `DB_GET_BOTH_RANGE`

  Useful for seeking the cursor to a specific record, regardless of whether it is a duplicate record. See Searching for Records for more information.

- `DB_NEXT_NODUP`, `DB_PREV_NODUP`

  Gets the next/previous non-duplicate record in the database. This allows you to skip over all the duplicates in a set of duplicate records. If you call `Dbc::get()` with `DB_PREV_NODUP`, then the cursor is positioned to the last record for the previous key in the database. For example, if you have the following records in your database:

      Alabama/Athens
      Alabama/Florence
      Alaska/Anchorage
      Alaska/Fairbanks
      Arizona/Avondale
      Arizona/Florence

  and your cursor is positioned to `Alaska/Fairbanks`, and you then call `Dbc::get()` with `DB_PREV_NODUP`, then the cursor is positioned to Alabama/Florence. Similarly, if you call `Dbc::get()` with `DB_NEXT_NODUP`, then the cursor is positioned to the first record corresponding to the next key in the database.

  If there is no next/previous key in the database, then `DB_NOTFOUND` is returned, and the cursor is left unchanged.

- `DB_NEXT_DUP`

  Gets the next record that shares the current key. If the cursor is positioned at the last record in the duplicate set and you call `Dbc::get()` with `DB_NEXT_DUP`, then `DB_NOTFOUND` is returned and the cursor is left unchanged.

For example, the following code fragment positions a cursor to a key and displays it and all its duplicates.

    #include <db_cxx.h>
    #include <string.h>

    ...

    char *search_key = "Al";

    Db my_database(NULL, 0);
    Dbc *cursorp;

    try {
        // database open omitted for clarity

        // Get a cursor
        my_database.cursor(NULL, &cursorp, 0);

        // Set up our DBTs
        Dbt key(search_key, strlen(search_key) + 1);
        Dbt data;

        // Position the cursor to the first record in the database whose
        // key and data begin with the correct strings.
        int ret = cursorp->get(&key, &data, DB_SET);
        while (ret != DB_NOTFOUND) {
            std::cout << "key: " << (char *)key.get_data()
                      << "data: " << (char *)data.get_data()<< std::endl;
            ret = cursorp->get(&key, &data, DB_NEXT_DUP);
        }
    } catch(DbException &e) {
            my_database.err(e.get_errno(), "Error!");
    } catch(std::exception &e) {
            my_database.errx("Error! %s", e.what());
    }

    // Close the cursor
    if (cursorp != NULL)
        cursorp->close();

    // Close the database
    my_database.close(0);

Prev | Up | Next
Chapter 4. Using Cursors | Home | Putting Records Using Cursors