如何使用SQLiteDatabase的方法replace() ?
在向DB编写代码时我有想在谷歌上搜索。 例如,在我的情况下,我有一个方法insert() e delete()分别用于在DB中插入或删除一个Note,但如果Note已经存在,我应该先调用delete()然后再调用insert()。
使用方法replace()我该怎么办?
如果有人会写一个例子,那将会很有帮助,谢谢。
How to use the method replace() of SQLiteDatabase?
While writing code to DB I had the idea to search on google. For example, in my case, I have an method insert() e delete() respectively for insert or remove one Note in DB, but if the Note already exist I should call first delete() and then insert().
While with method replace() how should I do?
It would be helpful if someone would write an example, thanks.
最满意答案
为了能够使用replace()方法,您需要在表上使用UNIQUE或PRIMARY KEY约束。
当您调用replace() ,数据库会尝试插入该行。 如果由于唯一性冲突而失败,则在插入新行之前会删除导致冲突的现有行。
如果要更新行, replace()将无用。
To be able to use the replace() method, you need a UNIQUE or PRIMARY KEY constraint on the table.
When you call replace(), the database tries to insert the row. If this fails because of a uniqueness conflict, the existing row that caused the conflict is deleted before inserting the new row.
replace() is useless if you want to update a row.
更多推荐
发布评论