从SQLite迁移到Android Room持久化库

3
我有一个应用程序,它使用 SQLite 数据库,我正在尝试将 Android SQLite 替换为 Android Room API。我已经创建了 DAO 类、实体类和数据库,但当在异步任务中执行数据库查询时,我会收到以下错误信息:
``` 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$1.doInBackground(HomeActivity.java:106) at com.rasfi.ai.ui.acivities.HomeActivity$1.doInBackground(HomeActivity.java:102) at android.os.AsyncTask$2.call(AsyncTask.java:304) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.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$3.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$1.run(AsyncTask.java:243) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) ```
我不明白我漏掉了什么,需要帮助。
1个回答

5
您正在尝试从SQLite数据库版本3转移到Room数据库版本1。虽然ROOM使用相同的数据库和版本控制(它只是SQLite上的抽象层),但您的Room不知道如何管理从版本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();

非常聪明。谢谢你。 - Taslim Oseni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接