public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder){ SQLiteDatabase database = mDbHelper.getReadableDatabase(); Cursor cursor; int match = sUriMatcher.match(uri); switch(match){ case PETS: // break; case PET_ID: selection = PetEntry._ID + "=?"; selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri))}; cursor = database.query(PetEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); break; default: throw new IllegalArgumentException("Cannot query unknown URI " + uri); } return cursor; }
public class PetProvider extends ContentProvider { @Override public Uri insert(Uri uri, ContentValues contentValues){ final int match = sUriMatcher.match(uri); switch (match){ case PETS: return insertPet(uri, contentValues); default: throw new IllegalArgumentException("Insertion is not supported for " + uri); } } private Uri insertPet(Uri uri, ContentValues values){ return ContentUris.withAppendedId(uri, id): } }
private Uri insertPet(Uri uri, ContentValues values){ String name = values.getAsString(PetEntry.COLUMN_PET_NAME); if (name == null){ throw new IllegalArgumentException("Pet requires a name"); } SQLiteDatabase database = mDbHelper.getWritableDatabse(); long id = database.insert(PetEntry.TABLE_NAME, null, values); if (id == -1){ Log.e(LOG_TAG, "Failed to insert row for " + uri); return null; } return ContentUris.withAppendedId(uri, id); }