Migrating from SQLite to Android Room Persistence Library

荒凉一梦 提交于 2020-01-03 03:27:05

问题


I have an app which is using SQLite database and I am trying to replace Android SQLite with Android Room API. I have created DAO class, Entity class, and DB but while executing database query in Async task I am getting following error:

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)

I am unable to understand what I am missing, any help would be appreciated.


回答1:


You are trying to move from SQLite db version 3 to Room db version 1. Sure the ROOM uses the same db and versioning(it is only abstraction layer over SQLite), your room doesn't know how to manage going from version 3 to 1.

You should increase version in Room to 4 and specify empty migration:

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

add it to room :

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


来源:https://stackoverflow.com/questions/48179598/migrating-from-sqlite-to-android-room-persistence-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!