Android SQLiteDatabase,如何使用replace()方法?
Android SQLiteDatabase, how to use method replace()?
如何使用SQLiteDatabase的方法replace()?
在向数据库编写代码时,我想到了在 google 上进行搜索。
例如,在我的例子中,我有一个方法 insert() e delete() 分别用于在数据库中插入或删除一个注释,但是如果注释已经存在我应该先调用 delete() 然后调用 insert()。
while with method replace() 我该怎么办?
如果有人能写个例子会很有帮助,谢谢。
为了能够使用 replace()
方法,您需要在 table.
上使用 UNIQUE 或 PRIMARY KEY 约束
当您调用 replace()
时,数据库会尝试插入该行。
如果由于唯一性冲突而失败,则在插入新行之前删除导致冲突的现有行。
如果要更新一行,replace()
没用。
如何使用SQLiteDatabase的方法replace()?
在向数据库编写代码时,我想到了在 google 上进行搜索。 例如,在我的例子中,我有一个方法 insert() e delete() 分别用于在数据库中插入或删除一个注释,但是如果注释已经存在我应该先调用 delete() 然后调用 insert()。
while with method replace() 我该怎么办?
如果有人能写个例子会很有帮助,谢谢。
为了能够使用 replace()
方法,您需要在 table.
当您调用 replace()
时,数据库会尝试插入该行。
如果由于唯一性冲突而失败,则在插入新行之前删除导致冲突的现有行。
replace()
没用。