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

Raj*_*era 18 sqlite android android-room

MainActivity类

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

}
Run Code Online (Sandbox Code Playgroud)

AppDatabase类

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

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);
}
Run Code Online (Sandbox Code Playgroud)

这是我第一次运行应用程序后出现错误.看起来该应用程序正在尝试再次创建DATABASE,但已经存在一个,因此他们建议更改版本代码.我的要求是我只需要插入一个新的数据集.我怎样才能做到这一点?提前致谢.这是logcat错误:

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.
Run Code Online (Sandbox Code Playgroud)

Sim*_*fer 35

Android 6.0+

对于那些现在遇到这种情况的人来说,简单地卸载可能没什么用,它会让你疯狂,所以在你必须解决之前,让我分享一下这个问题:Google在Android 6.0中引入了"自动备份"功能,重新安装时会返回一个数据库.(https://developer.android.com/guide/topics/data/autobackup.html)

你可以简单地添加......

<application ...
    android:allowBackup="false">
</app>
Run Code Online (Sandbox Code Playgroud)

...禁用此功能,你很高兴(现在你只需卸载即可删除数据库).


Vin*_*dar 24

如果您此刻只开发应用程序,则意味着您没有使用生产并从设备卸载应用程序并再次安装它将按要求运行.

  • 在奥利奥,无论我尝试什么,它似乎都不会消失. (16认同)
  • 只需清除应用程序数据即可 (5认同)

Hem*_*hik 8

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

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

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

希望这可以帮助某人;)


小智 6

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

1)如果你不想增加你的数据库版本并且你可以承受数据丢失 在设备中安装新构建后从应用程序设置中清除你的应用程序数据并再次启动应用程序,它会在没有数据完整性异常的情况下工作

2)如果您可以增加数据库版本但仍然可以承受数据丢失 如果您想使其无缝(不会因架构更新而崩溃),您可以简单地增加数据库版本(例如从1到2)并提及默认迁移Database Builder 中的策略fallbackToDestructiveMigration()。然后在安装此应用程序时,您之前的数据库数据将被清除,并且您不会收到此数据完整性异常。

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

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


Baf*_*tek 5

就我而言,重新安装没有帮助,而且我也无法设置,android:allowBackup:true因为我使用的库之一需要它。因此,我转到“设置”>“应用程序”>“我的应用程序”,并清除了该应用程序的数据和内存。问题消失了;)


小智 5

增加版本:

@Database(entities = {EmployeeEntry.class}, version = 2, exportSchema = false)
Run Code Online (Sandbox Code Playgroud)

链接获取