Android insertWithOnConflict returns wrong value

I am using the insertWithOnConflict function in Android / SQLite to insert values ​​that potentially already exist in the database. According to Android documentation, if this 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 automatically update the primary key and return this value, even if the data is not inserted into the string for the returned identifier.

The function call itself is relatively simple:

 name_id = db.insertWithOnConflict("names", null, name_values, SQLiteDatabase.CONFLICT_IGNORE); 
+4
source share
2 answers

happened to me. I also saw a few more cases of bizzare where it also answered me 0. It seems that there are some problems at the API level. Went back to insertOrThrow and then it worked for me sine.

+1
source

This is a known bug (and since 2010):
https://code.google.com/p/android/issues/detail?id=13045

And there are other problems with this API, for example,
https://code.google.com/p/android/issues/detail?id=3302

I believe the best solution is to simply avoid this API (as the Nazgul suggests).

0
source

Source: https://habr.com/ru/post/1388404/


All Articles