Reputation: 151
I'm trying to get the primary key (ID) created by the following insert:
public void insert (int FK_Vistoria, int CodIrregularidade, int FK_Equipamento, int FK_SubArea) throws SQLException {
String sql = "INSERT OR REPLACE INTO " + DATABASE_TABLE + " VALUES (?, ?, ?, ?);";
SQLiteStatement statement = mDb.compileStatement(sql);
mDb.beginTransaction();
statement.bindLong(1, FK_Vistoria);
statement.bindLong(2, FK_Equipamento);
statement.bindLong(3, CodIrregularidade);
statement.bindLong(4, FK_SubArea);
statement.execute();
mDb.setTransactionSuccessful();
mDb.endTransaction();
}
I tried do get it by using this piece of code:
int idIrregularidade = mDb.insert(idVistoria, idCriterio, idEquipamento, idArea);
Could someone help?
Upvotes: 1
Views: 546
Reputation: 132992
SQLiteStatement.execute() method return void
instead of inserted row id.
Use SQLiteStatement.executeInsert () which return the row ID of the last row inserted, if this insert is successful. -1 otherwise
Change insert
method as to get row ID on call of it:
public long insert (int FK_Vistoria,...){
....
long insertedRowID= statement.executeInsert();
....
return insertedRowID;
}
Upvotes: 2