Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room cannot verify the data integrity in Android

Tags:

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. 
like image 956
Rajitha Perera Avatar asked Jun 14 '17 11:06

Rajitha Perera


People also ask

Is Android an ORM room?

An easy way to use a database in an Android app is with a library called Room. Room is what's called an ORM (Object Relational Mapping) library, which as the name implies, maps the tables in a relational database to objects usable in Kotlin code.

What is the room database in Android?

What is Room? Room is a persistence library that provides an abstraction layer over the SQLite database to allow a more robust database. With the help of room, we can easily create the database and perform CRUD operations very easily.

Where are room databases stored android?

Although Room database file is located in the same partition as internal storage, it is located in different folder: Internal storage resides in files folder. Room database file resides in databases folder.

How do I delete data from a room database?

3.1 Add the Clear all data menu optionIn the Options menu, select Clear all data. All words should disappear. Restart the app. (Restart it from your device or the emulator; don't run it again from Android Studio) You should see the initial set of words.


2 Answers

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).

like image 135
Simon Mayrshofer Avatar answered Sep 28 '22 04:09

Simon Mayrshofer


If you're only developing the application at the moment, it means that you are not on Production and uninstalling the app from device and install it again will work as required.

like image 44
VinayagaSundar Avatar answered Sep 28 '22 04:09

VinayagaSundar