SQLite on Android: insertWithOnConflict () returns increasing results for the same datasets

BACKGROUND OF THE PROBLEM: I have two tables, where the main key is text fields.

When I reuse SQLiteDatabase.insertWithOnConflict(myTable, null, myValues, SQLiteDatabase.CONFLICT_REPLACE) with the same data, I see that all returned values โ€‹โ€‹are returned.

So, it looks like INSERT is executing (instead of REPLACE) for the same datasets.

Problem: However, looking at these tables using the "SQLite Database Browser", I see the correct number of records that I expect when executing REPLACE.

The root of my confusion is the description of the Android documentation, which says: * Returns the row identifier of the newly inserted row OR the primary key of the existing row if the input parameter is "Algorithm conflict" = CONFLICT_IGNORE OR -1 if any error *

Question: Why is REPLACE executed, but the row identifier of existing data is not returned?

+4
source share
2 answers

It seems to me that he is doing exactly what you are asking for? You ask him to be replaced, and he does just that. It will delete the old row and insert a new one.

It would leave the old string and return that identifier if you send CONFLICT_IGNORE .

+5
source
 try{ insertWithOnConflict(openHelper_ob.TABLE_NAME,null,contentValues, SQLiteDatabase.CONFLICT_ABORT); } catch(SQLException ex) { Toast.makeText(getApplicationContext(),"name found before plz, change it", Toast.LENGTH_SHORT).show(); } 
0
source

All Articles