Android Certification Training Course
- 64k Enrolled Learners
- Weekend
- Live Class
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.
The 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:
Course Name | Date | |
---|---|---|
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 |
edureka.co
best video i found in 2 days of searching .. thank you for you time
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.