SQLite on Android: insertWithOnConflict() returning ever-increasing results for same data sets
PROBLEM BACKGROUND: I have two tables where the Primary Key's are Text fields.
When I repeatedly use SQLiteDatabase.insertWithOnConflict(myTable, null, myValues, SQLiteDatabase.CONFLICT_REPLACE)
with the same data, I see ever increasing values being returned.
So it looks like INSERT is being performed (instead of REPLACE) for the same data sets.
PROBLEM: However, looking at these tables with "SQLite Database Browser" I see the correct number of records that I'd expect when REPLACE is performed.
The root to my confusion is the description Android documentation that states : *Returns the row ID of the newly开发者_Go百科 inserted row OR the primary key of the existing row if the input param 'conflictAlgorithm' = CONFLICT_IGNORE OR -1 if any error*
QUESTION: Why is it that a REPLACE is done, but the row ID of the existing data isn't returned ?
Seems to me like it is doing exactly what you are asking it to? You are asking it to replace and it does exactly that. It will remove the old row and insert the new one.
It would leave the old row and return that ID if you instead passed CONFLICT_IGNORE
.
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();
}
精彩评论