What is The use of moveToFirst () in SQLite Cursors

后端 未结 5 1068
花落未央
花落未央 2020-11-27 19:00

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_ROW         


        
5条回答
  •  野性不改
    2020-11-27 19:48

    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.

提交回复
热议问题