1

How can i find nth ROW from SQLiteDatabase in android, i want something like this SELECT * FROM Student WHERE rownum = 2; Here rownum is not a part of Table in my DB looks like

--------
Number
--------
123
456
789

So here i want to fetch row 456.

user1403653
  • 21
  • 1
  • 4

2 Answers2

2
Cursor c=db.rawQuery("SELECT * FROM Student", null);
                c.moveToFirst();
                index=c.getColumnIndex("rownum");
                x=Integer.parseInt(c.getString(index));
                while(index!=-1) 
                {
                    if(x==456)//or n
                    {

                        //do something

                    }
                    else{

                        c.moveToNext();
                        index=c.getColumnIndex("rownum");
                        x=Integer.parseInt(c.getString(index));

                    }
                }

EDIT:

Cursor c=db.rawQuery("SELECT * FROM Student", null);
c.moveToPosition(2);
jaisonDavis
  • 1,407
  • 2
  • 22
  • 36
  • if you want to select a number from a row then this is the most simplest way to do it – jaisonDavis Jun 21 '12 at 14:23
  • buddy but i want to get data which is on 2nd row coz database may be get updated so `456` order will be change but i always want 2nd record – user1403653 Jun 21 '12 at 14:27
1

You can try to add add LIMIT 1 and OFFSET <n> to the statement that you can execute like

Cursor c = db.rawQuery(SELECT_QUERY, null);
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106