安卓的ContentProvider - 插入方法中更新 [英] Android Contentprovider - update within an insert method

查看:151
本文介绍了安卓的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
    }
}

查看<一个href="http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html">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 - 插入方法中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆