Android and SQLite - retrieve max id from table Android and SQLite - retrieve max id from table sqlite sqlite

Android and SQLite - retrieve max id from table


Rewrite this line

id = mCursor.getInt(mCursor.getColumnIndex(MY_QUERY));

to

id = mCursor.getInt(mCursor.getColumnIndex("_id"));  

or better to

id = mCursor.getInt(0);//there's only 1 column in cursor since you only get MAX, not dataset

And look at LogCat, it will tell you about your problem.


final String MY_QUERY = "SELECT MAX(_id) FROM organize";

try only this


Try this method:

private int getMaxID(){    int mx=-1;    try{        db = this.getReadableDatabase();        SQLiteDatabase db=this.getReadableDatabase();        Cursor cursor=db.rawQuery("SELECT max(ID) from tblIntents ",new String [] {});        if (cursor != null)            if(cursor.moveToFirst())            {                mx= cursor.getInt(0);            }        //  cursor.close();        return mx;    }    catch(Exception e){        return -1;    }}