我在处理内容提供程序时遇到了问题。当我尝试通过内容提供程序更新 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/