Книга: Beginning Android

Using Cursors

Using Cursors

No matter how you execute the query, you get a Cursor back. This is the Android/SQLite edition of the database cursor, a concept used in many database systems. With the cursor, you can do the following:

• Find out how many rows are in the result set via getCount()

• Iterate over the rows via moveToFirst(), moveToNext(), and isAfterLast()

• Find out the names of the columns via getColumnNames(), convert those into column numbers via getColumnIndex(), and get values for the current row for a given column via methods like getString(), getInt(), etc.

• Re-execute the query that created the cursor, via requery()

• Release the cursor’s resources via close()

For example, here we iterate over the widgets table entries from the previous snippets:

Cursor result =
 db.rawQuery("SELECT ID, name, inventory FROM widgets");
result.moveToFirst();
while (!result.isAfterLast()) {
 int id = result.getInt(0);
 String name = result.getString(1);
 int inventory = result.getInt(2);
 // do something useful with these
 result.moveToNext();
}
result.close();

Оглавление книги


Генерация: 0.030. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз