Reputation: 461
I did some google search and i guess its duplicate, i want to put the value of max if in a variable, how to do that I have the below query:
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("select max(id) from tab1");
In oracle for example its like this
Select max(id) into numb_var from tab1
Upvotes: 1
Views: 1943
Reputation: 2707
try this
Cursor cursor = db.rawQuery("Select max(id) into numb_var from tab1",null);
if (cursor != null) {
cursor.moveToFirst();
while(!cursor.isAfterLast()){
maxID = cursor.getInt(0);
cursor.moveToNext();
}
cursor.close();
}
}
you'll have max in maxID variable.
Upvotes: 4
Reputation: 1166
Try this:
int id = 0;
Cursor c = db.rawQuery("select max(id) as id from tab1", null);
while(c.moveToNext()) {
id = c.getInt(0);
}
//you have your id stored in id variable
Upvotes: 2