What is use of Cursor in Android Development?
Asked Answered
O

7

79

I was going through some of code on the internet regarding the database connection, retrieval. I saw Cursor cur1= moveToFirst() in a lot of code and I want to know what the use of a cursor is and why we use moveToFirst().

Organic answered 30/3, 2012 at 7:10 Comment(1)
queception.com/question.php?question=106Ram
T
148

Cursor is the Interface which represents a 2 dimensional table of any database. When you try to retrieve some data using SELECT statement, then the database will first create a CURSOR object and return its reference to you.

The pointer of this returned reference is pointing to the 0th location which is otherwise called as before first location of the Cursor, so when you want to retrive data from the cursor, you have to first move to the first record so we have to use moveToFirst

When you invokes moveToFirst() method on the Cursor, it takes the cursor pointer to the first location. Now you can access the data present in the first record

Tumor answered 30/3, 2012 at 7:13 Comment(0)
C
67

In simple words, Cursor is a Interface whice returns collection of your query data. moveToFirst() is used to point the cursor position from where you want to get data from your cursor. There are methods moveToLast(), moveToNext(), moveToPrevious(), moveToPosition(position) by which you can iterate through your cursor by desired way.

For example, you have data in your Cursor

Lalit
Rithesh
Paresh
Chandra
  • moveToFirst() - If you use cursor.moveToFirst() then in this case it will point Lalit, as it is the first data in your cursor. To get the next data from cursor you can use moveToNext().

  • moveToLast() - This will point Chandra as the current data in your cursor. To get the previous data from cursor you can use moveToPrevious()

Counterinsurgency answered 30/3, 2012 at 7:17 Comment(0)
V
29

A Cursor represents the result of a query and basically points to one row of the query result. This way Android can buffer the query results efficiently; as it does not have to load all data into memory.

To get the number of elements of the resulting query use the getCount() method.

To move between individual data rows, you can use the moveToFirst() and moveToNext() methods. The isAfterLast() method allows to check if the end of the query result has been reached.

Cursor provides typed get*() methods, e.g. getLong(columnIndex), getString(columnIndex) to access the column data for the current position of the result. The "columnIndex" is the number of the column you are accessing.

Cursor also provides the getColumnIndexOrThrow(String) method which allows to get the column index for a column name of the table.

A Cursor needs to be closed with the close() method call. A query returns a Cursor object.

Vexatious answered 6/12, 2012 at 5:51 Comment(1)
I wonder if all the data has been fetched by the time the Cursor is created or if that may be delayed until you call methods like getCount() and moveTo*()Terisateriyaki
P
11

Cursor is like ResultSet in java, it has rows returned by some queries with its pointer. moveToFirst(), moveToNext() and moveToPosition(position) sets the pointer to desired postion.

Palestra answered 30/3, 2012 at 7:18 Comment(0)
A
4

A cursor is what any SQL query result will be stored in.

Acanthocephalan answered 4/1, 2017 at 8:31 Comment(1)
The explanation is not sufficientCalchas
L
3

Use the Cursor interface as a data collection.

It is similar to a Cursor in PL/SQL in the way that it holds one or more rows returned by some queries with its pointer.

The following methods are available in the Cursor interface which iterate through the Cursor, setting the Cursor pointer to the desired position:

  • moveToFirst()
  • moveToLast()
  • moveToNext()
  • moveToPrevious()
  • moveToPosition(position)
Lycopodium answered 17/2, 2016 at 8:54 Comment(0)
T
-1

Cursor interface provides random read-write access to the result set returned by a database query.

Cursor implementations are not required to be synchronized so code using a Cursor from multiple threads should perform its own synchronization when using the Cursor.

Tungstate answered 31/3, 2016 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.