开发者

Conflict resolution in Android's SQLiteDatabase

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 SQLiteDatab开发者_开发百科ase.insertOnConflict() method that was included starting in API level 8.


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.


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


By default i you have unique index exception will be thrown on duplicated index insert. Common solution is check record existence before execute insert - pseudo-code:

select * from people where name = 'Smith';
//if resultset is empty then perform insert
insert into people values 'Smith'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜