android - 内容提供者更新所有行

标签 android sqlite android-contentprovider

我在处理内容提供​​程序时遇到了问题。当我尝试通过内容提供程序更新 SQLite 数据库中的某一行时,它会更新所有行中的列,而不仅仅是我指定的行。我知道 CP 正在工作,因为我可以访问它,用它填充 ListView ,并更改列的内容,但绝不仅仅是一列。

这里是相关的更新方法

public int update(Uri url, ContentValues values, String where,
            String[] whereArgs) {
        SQLiteDatabase mDB = dbHelper.getWritableDatabase();
        int count;
        String segment = "";
        switch (URL_MATCHER.match(url)) {
        case ITEM:
            count = mDB.update(TABLE_NAME, values, where, whereArgs);
            break;
        case ITEM__ID:
            segment = url.getPathSegments().get(1);
            count = mDB.update(TABLE_NAME, values,
                    "_id="
                            + segment
                            + (!TextUtils.isEmpty(where) ? " AND (" + where
                                    + ')' : ""), whereArgs);
            break;

        default:
            throw new IllegalArgumentException("Unknown URL " + url);
        }
        getContext().getContentResolver().notifyChange(url, null);
        return count;
    }

这是我用来(尝试)更新它的代码。

ContentValues mUpdateValues = new ContentValues();

mUpdateValues.put(ContentProvider.HAS, "true");
mUpdateValues.put(ContentProvider.WANT, "false");

mRowsUpdated = getContentResolver().update(Uri.parse(ContentProvider._ID_FIELD_CONTENT_URI
+ rowId), mUpdateValues, null, null);

这是URI

URL_MATCHER.addURI(AUTHORITY, TABLE_NAME + "/#", ITEM__ID);

谢谢,我们将不胜感激。

编辑我也试过

mRowsUpdated = getContentResolver().update(
                    ContentProvider._ID_FIELD_CONTENT_URI, mUpdateValues,
                    null, null);

mRowsUpdated = getContentResolver().update(
                    ContentProvider.CONTENT_URI, mUpdateValues,
                    null, null);

最佳答案

您没有指定 WHERE 子句,该子句仅用于更新特定行。内容提供者的默认行为是更新所有行,除非您指定条件。

来自文档: developer.android.com/reference/android/content/ContentResolver.html

Parameters
uri     The URI to modify.
values  The new field values. The key is the column name for the field. A null value will remove an existing field value.
where   A filter to apply to rows before updating, formatted as an SQL WHERE clause (excluding the WHERE itself).

关于android - 内容提供者更新所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9933149/

相关文章:

android - 如何在viewpager中编写按钮的onClick方法?

android - Android Developer JSONObject 引用页中的标记值 NULL 是什么意思

Android java.lang.ClassCastException : android. widget.LinearLayout 无法转换为 android.widget.TextView

node.js - Sequelize hasMany 加入关联

c# - 无法确定 OR “Value cannot be null, parameter name ' 键的主体端'”

android 内容提供商在提供商崩溃时的稳健性

android - ndk-build *** 多目标模式。停止

mysql - 如何选择列中出现频率最高的 5 个值

android - 在 Android 上的 SQLite 中处理多个表 - 为什么这么复杂?

android - 删除数据库后 ContentProvider 未调用 onCreate