Android Development (55 Blogs) Become a Certified Professional

Introduction to Cursor in Android

Last updated on Nov 30,2020 29.6K Views


The basic purpose of a cursor is to point to a single row of the result fetched by the query. We load the row pointed by the cursor object. By using cursor we can save lot of ram and memory.

Android logo-EdurekaThe cursor is defined in the following code:

Public Cursor getString(String[] columnNames}{
Cursor cursor = database.query(table_name, columnName, null, null,
Null,null,null);
Return cursor;
}
}

Here, we pass the table name, column name only then we receive the cursor.

In order to get the columns and rows, we use the statement:

Int col = cursor.getColumnCount();
Int row =  cursor.getCount();
In order to get the column names, we use:
String[] columnNames = cursor.getColumnNames();

We then display the results through the statement:

Result += “No of Columns: “+col + “
”;
Result += “No of Rows: “+row + “
”;
Result += “Name of Columns” “+”
”;

Any query in the SQLite database returns a cursor object and the cursor points to a single row. Lets take the following code:

Cursor cursor = database.query(TABLE_NAME, columnNames, null, null, null, null, null);
Return cursor;
}
}

When we define the cursor it will point to the first row. Using the cursor we define a ‘for row’ with the code:

for( cursor.moveTofirst(); !cursor.isAfterLast(); cursor</pre>
<pre style="text-align: justify;">moveToNext()) {</pre>
<pre style="text-align: justify;">Result += cursor.getString(0) + “		”</pre>
<pre style="text-align: justify;">+ cursor.getString(1) + “		”</pre>
<pre style="text-align: justify;">+ cursor.getString(2) + “
”;</pre>
<pre style="text-align: justify;">}

When we call ‘Movetonext’ method it keeps going to the next row. If the Method is ‘afterlast’ it will go to NULL.

In order to check the database, we go to the File Explore tab and click the data folder. The folder ‘com.edureka.sqliteexample’ will have the db file.  We then use the SQLite manager and connect the database by loading the db file stored in the desktop. This will in turn show the database table. It will have elements such as row ID, name and numbers.

Got a question for us? Mention them in the comments section and we will get back to you.

Related Posts:

Beginner’s Guide to Android Architecture

Android Development Course

Upcoming Batches For Android Certification Training Course
Course NameDate
Android Certification Training Course

Class Starts on 4th March,2023

4th March

SAT&SUN (Weekend Batch)
View Details
Android Certification Training Course

Class Starts on 6th May,2023

6th May

SAT&SUN (Weekend Batch)
View Details
Comments
3 Comments
  • jason says:

    best video i found in 2 days of searching .. thank you for you time

  • geethadevi says:

    Finally,i fixed the problems about cursor in android..really well researched..Thank you for sharing..Android Training in velachery

  • This is a good introduction to Cursor. However, why do you say it returns only a single row? There can me many rows returned by the cursor, depending on your SQL query.

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Introduction to Cursor in Android

edureka.co