I'm trying to create methods for my DatabaseHandler class in Java to get the minimum and maximum row IDs from a single table in my database. I'm new to SQLite (working with Android) so I'm not sure what to do from here:
public int getMaxId() {
String countQuery = "SELECT last_insert_rowid() FROM " + TABLE_RESOURCES;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int maxId = <SOMETHING>
return maxId;
}
(I assume I could just change the sql query to 'first_insert_rowid()' for the first row ID - but how do I get and return that int?)
More background info: My table is constantly being written to and deleted from, so row IDs sometimes go like '2,3,4,5,8,9,11,13', and so on - and I'm running a for loop starting at the lowest row id available and going all the way to the highest, and ignoring empty spaces.
EDIT: Based on the answers, something like this? Only it gives a CursorOutOfBoundsException...
public int getMaxId() {
String countQuery = "SELECT MAX(rowid) FROM " + TABLE_RESOURCES;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int maxId = 0;
if (cursor.moveToFirst()) {
maxId = cursor.getInt(0);
return maxId;
} else
{
return maxId;
}
}