Implementing an Update Method

The update method in a content provider is responsible for updating a record based on the column values passed in, as well as the where clause that is passed in. The update method then returns the number of rows updated in the process.

Like the other methods, update uses UriMatcher to identify the URI type. If the URI type is a collection, the where clause is passed through so it can affect as many records as possible. If the URI type is a single-record type, then the book ID is extracted from the URI and specified as an additional where clause. In the end, the code returns the number of records updated (see Listing 3-29).

Listing 3-29. The updateO Method Implementation ^Override public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {

SOLiteDatabase db = mOpenHelper.getWritableDatabase(); int count;

switch (sUriMatcher.match(uri)) {

0 0

Post a comment