Книга: 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();
- Caveats using NAT
- Using Double Quotes to Resolve Variables in Strings with Embedded Spaces
- Data Binding Using the GridView Control
- Using the kill Command to Control Processes
- Installing Using a Network
- Using X
- Using a Display Manager
- Starting X from the Console by Using startx
- Using Fedora's switchdesk Client
- Using Environment Variables
- Using the Text Editors
- Using Man Pages