安卓的ContentProvider

编程入门 行业动态 更新时间:2024-10-27 02:20:53
本文介绍了安卓的ContentProvider - 插入方法中更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

它是确定调用在插入()覆盖内容提供商的方法 SQLiteDatabase 更新方法?

Is it ok to call the SQLiteDatabase update method in the insert() overridden method of a content provider?

推荐答案

基本上它是好的,但因为你没有提供的code,我只可以发布2可能的方法吧。

Basically it's fine, but since you didn't provided code, I just can post 2 possible ways for it

第一:

// In your content provider public Uri insert(...) { long insertId = db.insert(...); if(insertId == -1) { // insert failed, do update db.update(...); } }

二:

public Uri insert(...) { long insertId = db.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_REPLACE) if(insertId == -1) { // insert and update/replace failed } }

查看SQLiteDatabase关于第四参数参考。在大多数情况下,最后的方法应该是足够了,除非你想只有当行存在的某些字段被更新,并有各个领域,如果它没有。

Check out SQLiteDatabase for reference on the forth parameter. In most cases the last method should be sufficient, unless you want only certain fields being updated if the row exists and have all fields if it doesn't.

有关insertWithOnConflict最有用的需要而定,你可以将行,如果它已经存在,忽略插入,而是返回已有行的URI /主键。

Most useful need for insertWithOnConflict may be, that you could insert a row and if it already exists, ignore the insert and instead return the Uri/primary key of the already existing row.

更多推荐

安卓的ContentProvider

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

发布评论

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

>www.elefans.com

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