Using Cursors
No matter how you execute the query, you get a 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
• Iterate over the rows via , , and
• Find out the names of the columns via , convert those into column numbers via , and get values for the current row for a given column via methods like , , etc.
• Re‑execute the query that created the cursor, via
• Release the cursor’s resources via
For example, here we iterate over the table entries from the previous snippets:
RawQuery
MoveToFirst
While isAfterLast
GetInt
GetString
GetInt
// do something useful with these
MoveToNext
Close
Making Your Own Cursors
There may be circumstances in which you want to use your own subclass rather than the stock implementation provided by Android. In those cases, you can use and , which take a instance as a parameter. The factory, as one might expect, is responsible for creating new cursors via its implementation.
Finding and implementing a valid use for this facility is left as an exercise for the reader. Suffice it to say that you should not need to create your own cursor classes much, if at all, in ordinary Android development.
Дата добавления: 2015-05-16; просмотров: 875;