从 SQLite 迁移到 Android Room Persistence Library

Migrating from SQLite to Android Room Persistence Library

我有一个正在使用 SQLite 数据库的应用程序,我正在尝试用 Android Room API. 替换 Android SQLite 我已经创建了 DAO class,实体 class 和数据库,但在 Async 任务中执行数据库查询时出现以下错误:

Caused by: java.lang.IllegalStateException: A migration from 3 to 1 is necessary. Please provide a Migration in the builder or call fallbackToDestructiveMigration in the builder in which case Room will re-create all of the tables. at android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:82) at android.arch.persistence.room.RoomOpenHelper.onDowngrade(RoomOpenHelper.java:94) at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onDowngrade(FrameworkSQLiteOpenHelper.java:128) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:254) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:93) at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54) at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:193) at com.rasfi.ai.domain.room.RecordingDAO_Impl.getAll(RecordingDAO_Impl.java:112) at com.rasfi.ai.ui.acivities.HomeActivity.doInBackground(HomeActivity.java:106) at com.rasfi.ai.ui.acivities.HomeActivity.doInBackground(HomeActivity.java:102) at android.os.AsyncTask.call(AsyncTask.java:304) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:243)  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)  at java.lang.Thread.run(Thread.java:760)  01-10 08:13:37.648 30267-30339/com.rasfi.ai E/UncaughtException: java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask.done(AsyncTask.java:318) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:243) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)

我不明白我错过了什么,任何帮助将不胜感激。

您正在尝试从 SQLite 数据库版本 3 迁移到 Room 数据库版本 1。确定 ROOM 使用相同的数据库和版本控制(它只是 SQLite 之上的抽象层),您的房间不知道如何管理从版本 3 到 1.

您应该将 Room 中的版本增加到 4 并指定空迁移:

static final Migration MIGRATION_3_4 = new Migration(3, 4) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
// Since we didn't alter the table, there's nothing else to do here.
    }
};

将其添加到房间:

database = Room.databaseBuilder(context.getApplicationContext(),
        UsersDatabase.class, "Sample.db")
        .addMigrations(MIGRATION_3_4)
        .build();