Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conflict resolution in Android's SQLiteDatabase

Tags:

android

sqlite

How does Android's SQLite library resolve conflicts if I insert duplicate rows? I am building my app for API level 7, which unfortunately does not have the SQLiteDatabase.insertOnConflict() method that was included starting in API level 8.

like image 717
kibibyte Avatar asked Jul 07 '11 20:07

kibibyte


2 Answers

You can specify a UNIQUE index in the table definition which will allow rows to be REPLACED:

CREATE TABLE mytable (
  id  INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  UNIQUE (id) ON CONFLICT REPLACE
)

If a row an INSERT or UPDATE statement tries to add a row with an id which already exists, the existing row is replaced with the new one.

like image 136
David Snabel-Caunt Avatar answered Sep 18 '22 13:09

David Snabel-Caunt


There's a ON CONFLICT clause in SQLite that you can say INSERT INTO ... ON CONFLICT....

Read the documentation please. http://www.sqlite.org/lang_conflict.html

like image 44
ahmet alp balkan Avatar answered Sep 19 '22 13:09

ahmet alp balkan