Android - What is the difference between getReadableDatabase () and getWritableDatabase ()?

3

I was able to enter information into the database by using the getReadableDatabase() method. Should not that be a mistake then? Should not be the getWritableDatabase() method?

private void savePet() {

        petDbHelper = new PetDbHelper(this);
        db = petDbHelper.getReadableDatabase();

        ContentValues values = new ContentValues();
        values.put(PetEntry.COLUMN_PET_NAME, mNameEditText.getText().toString());
        values.put(PetEntry.COLUMN_PET_BREED, mBreedEditText.getText().toString());
        values.put(PetEntry.COLUMN_PET_WEIGHT, mWeightEditText.getText().toString());
        values.put(PetEntry.COLUMN_PET_GENDER, mGender);

        long newRowID =
                db.insert(PetEntry.TABLE_NAME, null, values);

        /** Display rowID after finish */
        Toast.makeText(this, "New row added: " + newRowID, Toast.LENGTH_SHORT).show();

        /** Close Activity */
        finish();
    }
    
asked by anonymous 24.02.2018 / 20:43

1 answer

4

According to the documentation , getReadableDatabase() :

  

Create and / or open a database. This will be the same thing returned   by getWritableDatabase () unless some problem, such as a full disk,   requires the database to be read-only

Translating:

  

Create or open a database. This will be the same object returned   by getWritableDatabase() , unless a problem occurs, such as   for example: if the disk is full the database opens in the   read-only mode.

But looking at the getWritableDatabase () documentation, she says that if the disk is full, this method will return an error.

Summarizing : Both return the same object, so there is no error. But you should use getWritableDatabase() to save data to be able to deal better with any errors that may arise.

    
24.02.2018 / 20:59