Bill Gary
Bill Gary

Reputation: 3005

Content provider updating all rows

working on a content provider and I'm having an issue with it. When I try to update a certain row in the SQLite database through the content provider, it updates the column in all the rows, not just the row I specify. I know the CP is working because I can access it, populate a listview with it, and change the content of column, but never just one column.

Here is the relevant update method

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;
    }

and here is the code I use to (try to) update it.

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);

and here is the URI

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

Thanks, any help would be appreciated.

EDIT I have also tried

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

and

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

Upvotes: 3

Views: 4288

Answers (1)

jsimpson
jsimpson

Reputation: 391

You are not specifying a WHERE clause, which is what is used to update only specific rows. The default behavior of content providers is to update all the rows, unless you specify conditions.

From the docs: 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).

Upvotes: 3

Related Questions