![]() ![]() Long Cursor.getLong(int columnIndex): Returns the value as a long Int Cursor.getInt(int columnIndex): Returns the value as an int ![]() The Cursor class contains the following methods for retrieving data from a row:īyte Cursor.getBlob(int columnIndex): Returns the value as a byteĭouble Cursor.getDouble(int columnIndex): Returns the value as a doubleįloat Cursor.getFloat(int columnIndex): Returns the value as a float The get() methods return the data from the column in the row which can then be used by the app. Once the column index is known, it can be passed to one of the cursor’s get() methods to return the typed data of the row. Instead, Cursor.getColumnIndex() returns a -1 value to represent an error. The Cursor class also contains a Cursor.getColumnIndex() method that does not throw an exception if the column name is not found. This usually indicates that the column was not part of the columns parameter of the query(). ![]() Cursor.getColumnIndexOrThrow()throws an exception if the column name does not exist in the cursor. Recall that the columns parameter determines what table columns are part of the result set. This String value needs to correspond to one of the strings in the columns parameter that was passed to the query() method. The Cursor.getColumnIndexOrThrow() method takes a String parameter that indicates which column to read from. To read the data, the code in Listing 5.10 uses two methods from the cursor class: Cursor.getColumnIndexOrThrow() and one of the type get() methods from the Cursor class. Once the cursor’s position is pointing to a valid row, the columns of the row can be read from the cursor. If other code can access the cursor (for example, if the cursor is passed into a method as a parameter), the cursor should also be set to a known position as the current position may not be the position ahead of the first row. This pattern is useful if the code performing the iteration “controls” the cursor and has sole access to it. The code in Listing 5.10 uses a while loop to iterate over the rows in the cursor returned from the query() method. Index = cursor.getColumnIndexOrThrow("id") ![]() String lastName = cursor.getString(index) Index = cursor.getColumnIndexOrThrow("last_name") String firstName = cursor.getString(index) Index = cursor.getColumnIndexOrThrow("first_name") Listing 5.10 Reading Cursor Data SQLiteDatabase db = getDatabase() Listing 5.10 shows the code to read data from a cursor containing all the data from the people table. This flag is useful for iterating over the rows in a cursor. The Cursor class provides the following methods to manipulate its internal position:īoolean Cursor.move(int offset): Moves the position by the given offsetīoolean Cursor.moveToFirst(): Moves the position to the first rowīoolean Cursor.moveToLast(): Moves the position to the last rowīoolean Cursor.moveToNext(): Moves the cursor to the next row relative to the current positionīoolean Cursor.moveToPosition(int position): Moves the cursor to the specified positionĬursor.moveToPrevious(): Moves the cursor to the previous row relative to the current positionĮach move() method returns a boolean to indicate whether the operation was successful or not. This means that before any rows of data can be read from the cursor, the position must be moved to point to a valid row of data. When a cursor is returned from a query() method, its position points to the spot before the first row of data. Internally, the cursor stores the rows of data returned by the query along with a position that points to the current row of data in the result set. Once a cursor has been returned from a database query, an app needs to iterate over the result set and read the column data from the cursor. The Cursor class has an API that allows an app to read (in a type-safe manner) the columns that were returned from the query as well as iterate over the rows of the result set. Cursors are what contain the result set of a query made against a database in Android. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |