Android SQLiteDatabase,如何使用方法replace()?(Android SQLiteDatabase, how to use method replace()?)

编程入门 行业动态 更新时间:2024-10-28 19:23:44
Android SQLiteDatabase,如何使用方法replace()?(Android SQLiteDatabase, how to use method replace()?)

如何使用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.

更多推荐

本文发布于:2023-04-27 22:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1328631.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使用方法   SQLiteDatabase   Android   replace   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!