Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite on Android: insertWithOnConflict() returning ever-increasing results for same data sets

Tags:

android

sqlite

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 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 ?

like image 836
Someone Somewhere Avatar asked Jun 13 '11 21:06

Someone Somewhere


People also ask

What are the several important methods that can be used in SQLite database for Android?

Important Methods in SQLite DatabaseThis method will return the number of rows in the cursor. This method returns a Boolean value when our cursor is closed. This method returns the total number of columns present in our table. This method will return the name of the column when we passed the index of our column in it.

How can I tell if data is installed in SQLite Android?

How to check whether the value is inserted or not in Android? if inserted==true return true; else return false; This is my code: SQLiteDatabase db = this.

What is the purpose of an SQLite database to an android application?

SQLite is an open-source relational database i.e. used to perform database operations on android devices such as storing, manipulating or retrieving persistent data from the database. It is embedded in android bydefault. So, there is no need to perform any database setup or administration task.

Which of the following is used to insert a data into the database in Android?

Android SQLite is the most preferred method of storing data in any android applications. Android SQLite is a relational database, therefore, It supports all types of relational database features.


1 Answers

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.

like image 160
alexanderblom Avatar answered Nov 01 '22 07:11

alexanderblom