Flutter Course (21 Blogs) Become a Certified Professional

Introduction to Cursor in Android

Last updated on Mar 15,2023 34.5K 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.

Discover how to create mobile apps that look and feel great on any platform with a comprehensive Flutter Training. 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

For details, You can even check various concepts related to Android app development and will help you become a certified Android developer with the Android app development course.

Comments
3 Comments

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