I’ve found this code sample online, but I can’t get it to work. When it runs, the app crashes. Is there any easy solution for this?
public long getMaxID() {
int id = 0;
final String MY_QUERY = "SELECT MAX(_id) AS _id FROM db_table";
Cursor mCursor = mDb.rawQuery(MY_QUERY, null);
if (mCursor.getCount() > 0) {
mCursor.moveToFirst();
id = mCursor.getInt(mCursor.getColumnIndex(MY_QUERY));
}
return id;
}
My logcat error seems to be:
10-21 07:48:32.704: E/AndroidRuntime(4023): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
You cannot do
getColumnIndexon some query string, you need to pass the column name which is _id. So instead ofUse