Opening/closing the database twice is inefficient; and there is a helper to read a single value from a query.
Anyway, you could move the computations into SQL:
db = getWritableDatabase();
try {
long id = DBUtils.longForQuery(db,
"SELECT IFNULL(MIN(id) - 1, 0) FROM MyTable", null);
cv.put("id", id);
...
} finally {
db.close();
}
1
solved Android SQLite – Primary Key Desc [closed]