app/src/main/java/com/example/android/pets/data/PetProvider.java
/**
     * Insert a pet into the database with the given content values. Return the new content URI
     * for that specific row in the database.
     */
Insert a pet into the database with the given content values. Return the new content URI
for that specific row in the database.
Insert a pet into the database with the given content values . Return the new content URI for that specific row in the database .
private Uri insertPet(Uri uri, ContentValues values) {
        String name = values.getAsString(PetContract.PetEntry.COLUMN_PET_NAME);
        if (name == null) {
            throw new IllegalArgumentException("Pet requires a name");
        }
        Integer gender = values.getAsInteger(PetContract.PetEntry.COLUMN_PET_GENDER);
        if (gender == null || !PetContract.PetEntry.isValidGender(gender)) {
            throw new IllegalArgumentException("Pet requires valid gender");
        }
        Integer weight = values.getAsInteger(PetContract.PetEntry.COLUMN_PET_WEIGHT);
        if (weight != null && weight < 0) {
            throw new IllegalArgumentException("Pet requires valid weight");
        }
        SQLiteDatabase database = mDbHelper.getWritableDatabase();
        long id = database.insert(PetContract.PetEntry.TABLE_NAME, null, values);
        if (id == -1) {
            Log.e(LOG_TAG, "Failed to insert row for " + uri);
            return null;
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return ContentUris.withAppendedId(uri, id);
    }
private Uri insertPet ( Uri uri , ContentValues values ) { String name = values . getAsString ( PetContract . PetEntry . COLUMN_PET_NAME ) ; if ( name == null ) { throw new IllegalArgumentException ( "Pet requires a name" ) ; } Integer gender = values . getAsInteger ( PetContract . PetEntry . COLUMN_PET_GENDER ) ; if ( gender == null || ! PetContract . PetEntry . isValidGender ( gender ) ) { throw new IllegalArgumentException ( "Pet requires valid gender" ) ; } Integer weight = values . getAsInteger ( PetContract . PetEntry . COLUMN_PET_WEIGHT ) ; if ( weight != null && weight < 0 ) { throw new IllegalArgumentException ( "Pet requires valid weight" ) ; } SQLiteDatabase database = mDbHelper . getWritableDatabase ( ) ; long id = database . insert ( PetContract . PetEntry . TABLE_NAME , null , values ) ; if ( id == - 1 ) { Log . e ( LOG_TAG , "Failed to insert row for " + uri ) ; return null ; } getContext ( ) . getContentResolver ( ) . notifyChange ( uri , null ) ; return ContentUris . withAppendedId ( uri , id ) ; }
Insert a pet into the database with the given content values.
Insert a pet into the database with the given content values .
// Check that the name is not null // Check that the gender is valid // If the weight is provided, check that it's greater than or equal to 0 kg // No need to check the breed, any value is valid (including null). // Get writeable database // Insert the new pet with the given values // If the ID is -1, then the insertion failed. Log an error and return null. // Notify all listeners that the data has changed for the pet content uri // uri: content//com.example.android.pets/pets // null = optional content observer, if null is passed, cursor adapter will still get notified // Return the new URI with the ID (of the newly inserted row) appended at the end