@Override public int delete(Uri uri, String selection, String[] selectionArgs) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); int numDeleted; switch (match) { case FAVORITE_MOVIE: numDeleted = db.delete( FavoriteMoviesContract.FavoriteMovieEntry.TABLE_FAVORITE_MOVIES, selection, selectionArgs); // reset _ID db.execSQL( "DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + FavoriteMoviesContract.FavoriteMovieEntry.TABLE_FAVORITE_MOVIES + "'"); break; case FAVORITE_MOVIE_WITH_ID: numDeleted = db.delete( FavoriteMoviesContract.FavoriteMovieEntry.TABLE_FAVORITE_MOVIES, FavoriteMoviesContract.FavoriteMovieEntry._ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}); break; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } return numDeleted; }
@Override public Uri insert(Uri uri, ContentValues values) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); Uri returnUri; switch (sUriMatcher.match(uri)) { case FAVORITE_MOVIE: { long _id = db.insert( FavoriteMoviesContract.FavoriteMovieEntry.TABLE_FAVORITE_MOVIES, null, values); // insert unless it is already contained in the database if (_id > 0) { returnUri = FavoriteMoviesContract.FavoriteMovieEntry.buildMoviesUri(_id); } else { throw new android.database.SQLException("Failed to insert row into: " + uri); } break; } default: { throw new UnsupportedOperationException("Unknown uri: " + uri); } } getContext().getContentResolver().notifyChange(uri, null); return returnUri; }
@Override public int bulkInsert(Uri uri, ContentValues[] values) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); switch (match) { case FAVORITE_MOVIE: // allows for multiple transactions db.beginTransaction(); // keep track of successful inserts int numInserted = 0; try { for (ContentValues value : values) { if (value == null) { throw new IllegalArgumentException("Cannot have null content values"); } long _id = -1; try { _id = db.insertOrThrow( FavoriteMoviesContract.FavoriteMovieEntry.TABLE_FAVORITE_MOVIES, null, value); } catch (SQLiteConstraintException e) { Log.w( LOG_TAG, "Attempting to insert " + value.getAsString(FavoriteMoviesContract.FavoriteMovieEntry.COLUMN_TITLE) + " but value is already in database."); } if (_id != -1) { numInserted++; } } if (numInserted > 0) { // If no errors, declare a successful transaction. // database will not populate if this is not called db.setTransactionSuccessful(); } } finally { // all transactions occur at once db.endTransaction(); } if (numInserted > 0) { // if there was successful insertion, notify the content resolver that there // was a change getContext().getContentResolver().notifyChange(uri, null); } return numInserted; default: return super.bulkInsert(uri, values); } }
@Override public int update( Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); int numUpdated = 0; if (contentValues == null) { throw new IllegalArgumentException("Cannot have null content values"); } switch (sUriMatcher.match(uri)) { case FAVORITE_MOVIE: { numUpdated = db.update( FavoriteMoviesContract.FavoriteMovieEntry.TABLE_FAVORITE_MOVIES, contentValues, selection, selectionArgs); break; } case FAVORITE_MOVIE_WITH_ID: { numUpdated = db.update( FavoriteMoviesContract.FavoriteMovieEntry.TABLE_FAVORITE_MOVIES, contentValues, FavoriteMoviesContract.FavoriteMovieEntry._ID + " = ?", new String[] {String.valueOf(ContentUris.parseId(uri))}); break; } default: { throw new UnsupportedOperationException("Unknown uri: " + uri); } } if (numUpdated > 0) { getContext().getContentResolver().notifyChange(uri, null); } return numUpdated; }