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.

2 Answers 2

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); 
Sign up to request clarification or add additional context in comments.

2 Comments

if you want to select a number from a row then this is the most simplest way to do it
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
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); 

6 Comments

Cursor c = myDB.rawQuery("SELECT * FROM " + TableName + "LIMIT 1;", null); but this is not working
You are missing space before limit. try to use offset yet.
i dont have tio specify buddy thats a problem
so when you add LIMIT 1 OFFSET 30, it will select 31st row. you must specify which row you want to select...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.