I\'m using the SQLite with Android, and I want to know the best way to get the generated id of the row I inserted.
A solution I think makes a search after include, b
One can simply get last inserted row _id using last_insert_rowid(). Sample code is as below.
/**
* Return Last inserted row id(auto incremented row) (_id)
* @return
*/
public int getLastAddedRowId() {
String queryLastRowInserted = "select last_insert_rowid()";
final Cursor cursor = database.rawQuery(queryLastRowInserted, null);
int _idLastInsertedRow = 0;
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
_idLastInsertedRow = cursor.getInt(0);
}
} finally {
cursor.close();
}
}
return _idLastInsertedRow;
}