# Interface Cursor

public interface Cursor

The Cursor interface is used to iterate over the results returned from a database query. IMPORTANT: Notice that some methods might not be supported on all platforms!

There is more thorough coverage of the `Database API here`.

The sample code below presents a Database Explorer tool that allows executing arbitrary SQL and viewing the tabular results:

    Toolbar.setGlobalToolbar(true);
    Style s = UIManager.getInstance().getComponentStyle("TitleCommand");
    FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_QUERY_BUILDER, s);
    Form hi = new Form("SQL Explorer", new BorderLayout());
    hi.getToolbar().addCommandToRightBar("", icon, (e) -> {
        TextArea query = new TextArea(3, 80);
        Command ok = new Command("Execute");
        Command cancel = new Command("Cancel");
        if(Dialog.show("Query", query, ok, cancel) == ok) {
            Database db = null;
            Cursor cur = null;
            try {
                db = Display.getInstance().openOrCreate("MyDB.db");
                if(query.getText().startsWith("select")) {
                    cur = db.executeQuery(query.getText());
                    int columns = cur.getColumnCount();
                    hi.removeAll();
                    if(columns > 0) {
                        boolean next = cur.next();
                        if(next) {
                            ArrayList data = new ArrayList<>();
                            String[] columnNames = new String[columns];
                            for(int iter = 0 ; iter

    @author Chen

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `close()`
  Close the cursor and release its resources
  `boolean`
  `first()`
  Move the cursor to the first row.
  `int`
  `getColumnCount()`
  Returns the column count
  `int`
  `getColumnIndex(String columnName)`
  Returns the zero-based index for a given column name.
  `String`
  `getColumnName(int columnIndex)`
  Returns the column name at a given zero-based column index.
  `int`
  `getPosition()`
  Returns the current Cursor position.
  `Row`
  `getRow()`
  Get the Row data Object.
  `boolean`
  `last()`
  Move the cursor to the last row.
  `boolean`
  `next()`
  Moves the cursor to the next row.
  `boolean`
  `position(int row)`
  Move the cursor to an absolute row position
  `boolean`
  `prev()`
  Moves the cursor to the previous row.

- ## Method Details

  - ### first

    boolean first() throws IOException

    Move the cursor to the first row.

    If cursor provides forward-only navigation and is positioned after the first row then calling first() method would throw a IOException.

    ###### Returns

    true if succeeded

    ###### Throws

    - `IOException`
    Throws:
      `IOException`

  - ### last

    boolean last() throws IOException

    Move the cursor to the last row.

    ###### Returns

    true if succeeded

    ###### Throws

    - `IOException`
    Throws:
      `IOException`

  - ### next

    boolean next() throws IOException

    Moves the cursor to the next row. Calling next() method the first time will position cursor on the first.

    ###### Returns

    true if succeeded

    ###### Throws

    - `IOException`
    Throws:
      `IOException`

  - ### prev

    boolean prev() throws IOException

    Moves the cursor to the previous row. If cursor is forward type then calling prev() would throw a IOException.

    ###### Returns

    true if succeeded

    ###### Throws

    - `IOException`
    Throws:
      `IOException`

  - ### getColumnIndex

    int getColumnIndex(String columnName) throws IOException

    Returns the zero-based index for a given column name. Note that columns meta information is available only after navigation to the first row

    ###### Parameters

    - `columnName`: the name of the column.

    ###### Returns

    the index of the column

    ###### Throws

    - `IOException`
    Throws:
      `IOException`

  - ### getColumnName

    String getColumnName(int columnIndex) throws IOException

    Returns the column name at a given zero-based column index. Note that columns meta information is available only after navigation to the first row

    ###### Parameters

    - `columnIndex`: the index of the column

    ###### Returns

    the name of the column

    ###### Throws

    - `IOException`
    Throws:
      `IOException`

  - ### getColumnCount

    int getColumnCount() throws IOException

    Returns the column count

    ###### Returns

    the column count

    ###### Throws

    - `IOException`
    Throws:
      `IOException`

  - ### getPosition

    int getPosition() throws IOException

    Returns the current Cursor position.

    ###### Returns

    the cursor position

    ###### Throws

    - `IOException`
    Throws:
      `IOException`

  - ### position

    boolean position(int row) throws IOException

    Move the cursor to an absolute row position

    ###### Parameters

    - `row`: position to move to

    ###### Returns

    true if succeeded

    ###### Throws

    - `IOException`
    Throws:
      `IOException`

  - ### close

    void close() throws IOException

    Close the cursor and release its resources

    ###### Throws

    - `IOException`
    Throws:
      `IOException`

  - ### getRow

    Row getRow() throws IOException

    Get the Row data Object.

    ###### Returns

    a Row Object

    ###### Throws

    - `IOException`
    Throws:
      `IOException`