Baalback
Baalback

Reputation: 461

get the max of id and store value in sqlite db

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

Answers (2)

Chintan Desai
Chintan Desai

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

aletede91
aletede91

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

Related Questions