android – What is The use of moveToFirst () in SQLite Cursors-ThrowExceptions

Exception or error:

I am a programming newbie
and I found this piece of code in the internet and it works fine

Cursor c=db.query(DataBase.TB_NAME, new String[] {DataBase.KEY_ROWID,DataBase.KEY_RATE}, DataBase.KEY_ROWID+"= 1", null, null, null, null);
        if(c!=null)
        {
            c.moveToFirst();
        }

but I am not able to understand the use of the

if(c!=null)
    {
        c.moveToFirst();
    }

part. What does it do exactly , and if I remove the

if(c!=null) { c.moveToFirst(); }

part, the code doesn’t work.

How to solve:

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.

###

if (c.moveToFirst()) {
  while(!c.isAfterLast()) { // If you use c.moveToNext() here, you will bypass the first row, which is WRONG
    ...
    c.moveToNext();
  } 
}

###

Cursor is not a Row of the result of query. Cursor is an object that can iterate on the result rows of your query. Cursor can moves to each row. .moveToFirst() method move it to the first row of result table.

###

moveToFirst() method moves the cursor to the first row. It allows to perform a test whether the query returned an empty set or not. Here is a sample of its implementation,

if (cursor.getCount() == 0 || !cursor.moveToFirst()) {

return cursor.getLong(cursor.getColumnIndexOrThrow(ID_COLUMN)); 

cursor.close(); 

###

what macio.Jun says is right!

we have code like below:

    String sql = "select id,title,url,singer,view,info from cache where id=" + id;
    SQLiteDatabase db = getMaintainer().getReadableDatabase();
    Cursor query = db.rawQuery(sql, null);
    query.moveToFirst(); 
    while(query.moveToNext()){
        DBMusicData entity = new DBMusicData();
        entity.setId(query.getString(query.getColumnIndex(FIELD_ID)));
        entity.setTitle(query.getString(query.getColumnIndex(FIELD_TITLE)));
        entity.setSinger(query.getString(query.getColumnIndex(FIELD_SINGER)));
        entity.setTitlepic(query.getString(query.getColumnIndex(FIELD_PICURL)));
        entity.setInfoUrl(query.getString(query.getColumnIndex(FIELD_INFO)));
        entity.setViews(query.getString(query.getColumnIndex(FIELD_VIEW)));
        Log.w(tag, "cache:"+ entity.toString());
    }
    query.close();
    query=null;
    db.close();
    db=null;

If we have only one record in the cache table, query.moveToFirst(); will cause that no record returns.

Leave a Reply

Your email address will not be published. Required fields are marked *