What Is the Use of Movetofirst () in SQLite Cursors

What is The use of moveToFirst () in SQLite Cursors

The docs for SQLiteDatabase.query() say that the query methods return:

"A Cursor object, which is positioned before the first entry."

Calling moveToFirst() does two things: it allows you to test whether the query returned an empty set (by testing the return value) and it moves the cursor to the first result (when the set is not empty). Note that to guard against an empty return set, the code you posted should be testing the return value (which it is not doing).

Unlike the call to moveToFirst(), the test for if(c!=null) is useless; query() will either return a Cursor object or it will throw an exception. It will never return null.

android cursor.moveToNext()?

For clarity a complete example would be as follows which I trust is of interest. As code comments indicated we essentially iterate over database rows and then columns to form a table of data as per database.

    Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null,
null);

//if the cursor isnt null we will essentially iterate over rows and then columns
//to form a table of data as per database.
if (cursor != null) {

//more to the first row
cursor.moveToFirst();

//iterate over rows
for (int i = 0; i < cursor.getCount(); i++) {

//iterate over the columns
for(int j = 0; j < cursor.getColumnNames().length; j++){

//append the column value to the string builder and delimit by a pipe symbol
stringBuilder.append(cursor.getString(j) + "|");
}
//add a new line carriage return
stringBuilder.append("\n");

//move to the next row
cursor.moveToNext();
}
//close the cursor
cursor.close();
}

Why cursor.moveToFirst() should be called while we using cursor?

The Cursor you get from query starts out before the first record.

So in order to use it, you need to put it somewhere.

Cursor Index Out Of Bounds Exception (android SQLite)

Try this:

Cursor c = hlp.getData(Email);
if(c != null)
c.moveToFirst();

tv3 = (TextView) findViewById(R.id.tv3);
tv3.setText(c.getString(c.getColumnIndex(HelperDB.NOTES)));

cursor.moveToFirst() is returning false

if cursor.moveToFirst() returns false, is because you are not getting results.

You have an error on your insert method, and i believe that your table is empty.

Your table has a column named "Release_Date" but in your insert_data method you are using an incorrect name:

  movie_data.put("Release Date",release_date);

You are missing a "_"

 movie_data.put("Release_Date",release_date);

This is probably making your insert queries fail and that's why you are not getting results.

EDIT: Although i don't know what you are trying to achieve with your queries, the .query() method has a param for ordering ASC or DESC your results. So, you could remove your arrange method.

Example for one of your queries with ascendent order by column "Movie_Name":

   Cursor cursor = db.query("data",
new String[]{"_id", "Movie_Name"},
null,
null, null, "Movie_Name ASC", null
);

Check the doc:
https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

What to do when cursor.movetofirst is false (SQLite)

Per the Android documents:

This method will return false if the cursor is empty.

So you must not have any data there, and you should manage appropriately. Check for typos, etc.

why c.moveToFirst() won't skip the first row

You're calling moveToFirst(), which positions the cursor on the first row, then your while loop calls moveToNext() before entering the loop, which results in the first row being skipped.

You can solve it with either a do...while loop, or a for loop such as

for( c.moveToFirst(); !c.isAfterLast(); c.moveToNext() ) {
// Stuff
}


Related Topics



Leave a reply



Submit