I’m using the insertWithOnConflict function in Android/SQLite to insert values that potentially already exist in the database. According to the Android documentation if the value exists, the primary key will be returned:
Returns
the row ID of the newly inserted row OR the primary key of the existing row if
the input param 'conflictAlgorithm' = CONFLICT_IGNORE OR -1 if any error
However, when using this function, the existing primary key is not returned if the row already exists. Instead, it will autoincrement the primary key and return that value, even though no data is inserted into the row for the id that is returned.
The function call itself is relatively simple:
name_id = db.insertWithOnConflict("names", null, name_values, SQLiteDatabase.CONFLICT_IGNORE);
happened with me also. I also saw a few more bizzare cases where it returned me 0 also. Seems there is some problem in the API level. Have reverted back to insertOrThrow and that has worked for me sine then.