Room 无法验证 Android 中的数据完整性

Room cannot verify the data integrity in Android

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.

如果您目前只是在开发应用程序,则意味着您未处于生产状态,从设备上卸载应用程序并重新安装即可按要求进行。

Android 6.0+

对于那些现在遇到这个问题的人来说,简单地卸载可能无济于事,它会让你发疯,所以在你弄清楚之前,让我分享一下这个问题:Google 介绍中的“自动备份”功能 Android 6.0,重新安装时会带回一个数据库。 (https://developer.android.com/guide/topics/data/autobackup.html)

您可以简单地添加...

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

...禁用此功能,一切顺利(现在您只需卸载即可删除数据库)。

如果您不关心更新实体时的迁移:

  1. 增加数据库版本
  2. 建库时添加fallbackToDestructiveMigration()方法,例如:

    Room.databaseBuilder(appContext, AppDatabase.class, "appdb.db") .fallbackToDestructiveMigration() .build();

希望这对某人有所帮助 ;)

就我而言,重新安装没有帮助,而且我无法设置 android:allowBackup:true,因为我使用的其中一个库需要它。所以我转到“设置”>“应用”>“我的应用”,并清除了该应用的数据和内存。问题解决了;)

增加版本:

@Database(entities = {EmployeeEntry.class}, version = 2, exportSchema = false)

取自 link

第 1 步:安装新版本

第 2 步:清除“设置”中的所有应用数据。

第 3 步:打开应用程序(现在可以正常工作)

有 3 种方法可以修复此数据完整性异常。

1) 如果您不想增加数据库版本并且可以承受数据丢失 安装新的内置设备后,只需从应用程序设置中清除您的应用程序数据,然后再次启动应用程序,它就可以正常工作而不会出现数据完整性异常

2) 如果你可以提高你的数据库版本,但你仍然可以忍受数据丢失 如果你想让它无缝(不会因为模式更新而崩溃),你可以简单地增加数据库版本(比如从 1 到 2)并在数据库构建器中提及默认迁移策略 fallbackToDestructiveMigration()。然后在安装这个应用程序时,您以前的数据库数据将被清除,您不会得到这个数据完整性异常。

3) 如果你可以增加你的数据库版本并且还想保存你以前的数据 您需要增加您的数据库版本(比如从 1 到 2),但还需要在您的应用程序中实施适当的迁移逻辑(基于您的较低版本和较高版本)。然后在安装新版本时,您将不会收到此数据完整性异常。 对于 Room 的迁移,您可以阅读 this 为迁移写的好文章

有关 Room 迁移的官方文档,请转到 link

您也可以在终端中使用 adb shell pm uninstall com.package.app 完全卸载该应用程序。之后重新安装,错误消失