Room cannot verify the data integrity in Android

Rajitha Perera picture Rajitha Perera · Jun 14, 2017 · Viewed 21k times · Source

MainActivity class

public class MainActivity extends BaseActivity {

    private AppDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Database creation
        db = Room.databaseBuilder(getApplicationContext(),
                AppDatabase.class, "Medimap-db").build();
//        Profile profile=new Profile("rajitha","12-2-345","male","no 345","test med","test emergency","testurl");
//        db.profileDao().insert(profile);
//        Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
        new DatabaseAsync().execute();


    }

    private class DatabaseAsync extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //Perform pre-adding operation here.
        }

        @Override
        protected Void doInBackground(Void... voids) {
            //Let's add some dummy data to the database.
            Profile profile = new Profile("rajitha", "12-2-345", "male", "no 345", "test med", "test emergency", "testurl");
            db.profileDao().insert(profile);


            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_SHORT).show();
            //To after addition operation here.
        }
    }

}

AppDatabase class

   @Database(entities = {Profile.class, Medicine.class}, version = 1)
    public abstract class AppDatabase extends RoomDatabase {
        public abstract ProfileDao profileDao();
        public abstract MedicineDao medicineDaoDao();
    }

ProfileDao

@Dao
public interface ProfileDao {

    @Query("SELECT * FROM profile")
    List<Profile> getAll();

//    @Query("SELECT * FROM user WHERE uid IN (:userIds)")
//    List<Profile> loadAllByIds(int[] userIds);

//    @Query("SELECT * FROM user WHERE first_name LIKE :first AND "
//            + "last_name LIKE :last LIMIT 1")
//    User findByName(String first, String last);

    @Insert
    void insertAll(Profile... profiles);

    @Insert
    void insert(Profile profile);

    @Delete
    void delete(Profile profile);
}

Here I got an error after the first run of the app. It seems like the app is trying to create DATABASE once again, but there already is an existing one so they suggested to change the version code. My requirement is that I only need to insert a new data set. How can I achieve that? Thanks in advance. Here is the logcat error:

Caused by: java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.

Answer

Simon Mayrshofer picture Simon Mayrshofer · Feb 2, 2018

Android 6.0+

For those of you who come across this now a days, simply uninstalling might not help and it will drive you nuts, so before you have to figure it out, let me share the issue: Google introduced the "autobackup" functionality in Android 6.0, which brings back a DB when you reinstall. (https://developer.android.com/guide/topics/data/autobackup.html)

You can simply add...

<application ...
    android:allowBackup="false">
</app>

...to disable this functionality and you are good to go (now you can simply uninstall to delete the database).