如何将Android SqLite数据库从内部存储迁移到Room?

时间:2018-05-15 06:33:37

标签: android android-sqlite android-room

我的应用程序具有版本1的Sqlite数据库文件。这是使用 SqliteOpenHelper 类创建的。

现在我想将旧数据库迁移到房间数据库。

以下是我的代码,似乎对我不起作用。

@ApplicationScope
@Provides
AppDatabase provideDatabase(Application application) {
    String dbPath = "";
    if (application.getExternalFilesDir(null) != null) {
        dbPath = application.getExternalFilesDir(null).getPath();
    } else {
        dbPath = application.getFilesDir().getPath();
    }

    return Room.databaseBuilder(application, AppDatabase.class, dbPath + File.separator + DB_NAME)
            .addMigrations(RoomDatabaseMigrationProvider.MIGRATION_1_2)
            .build();
}

以上代码未调用 RoomDatabaseMigrationProvider.MIGRATION_1_2 。我还尝试添加 RoomDatabase.Callback 。它在void onCreate(@NonNull SupportSQLiteDatabase db)而不是版本1中返回0,在void onOpen(@NonNull SupportSQLiteDatabase db)方法中返回其返回版本2.

以下是我的AppDatabase.java和Migration类

@Database(entities = {Calendar.class, Contest.class},
    version = 2, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    public abstract ContestDao provideContestDao();

    public abstract CalanderDao provideCalanderDao();

}

public static final Migration MIGRATION_1_2 = new Migration(1, 2) {

    @Override
    public void migrate(@NonNull final SupportSQLiteDatabase database) {
        Log.d("##### Migration", "started....");
        migrateDatabase(database);
        Log.d("##### Migration", "completed....");
    }
};

2 个答案:

答案 0 :(得分:0)

试试这个

   @Database(entities = {<all entity classes>}, 
              version = <incremented_sqlite_version>)
    public abstract class AppDatabase extends RoomDatabase {
        private static UsersDatabase INSTANCE;
        static final Migration      MIGRATION_<sqlite_version>_<incremented_sqlite_version> 
    = new Migration(<sqlite_version>, <incremented_sqlite_version>) {
             @Override public void migrate(
                        SupportSQLiteDatabase database) {
               // Since we didn’t alter the table, there’s nothing else 
               // to do here.
             }
        };

供参考:

https://medium.com/google-developers/incrementally-migrate-from-sqlite-to-room-66c2f655b377

答案 1 :(得分:0)

由于 PRAGMA user_version ,迁移无效。

我使用SQLite浏览器软件创建了一个SQLite数据库,默认情况下它将0设置为 user_version

这就是我在void onCreate(@NonNull SupportSQLiteDatabase db)方法中获得 0 的原因。

以下是我用来解决此问题的代码。

@ApplicationScope
@Provides
AppDatabase provideDatabase(Application application) {
    String dbPath;
    if (application.getExternalFilesDir(null) != null) {
        dbPath = application.getExternalFilesDir(null).getPath();
    } else {
        dbPath = application.getFilesDir().getPath();
    }
    dbPath = dbPath + File.separator + DB_NAME;

    try { 
        SQLiteDatabase db = SQLiteHelper.getSqliteHelper(application).getWritableDatabase();
        if (db != null && db.getVersion() == 0) {
            db.execSQL("PRAGMA user_version = 1");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Room.databaseBuilder(application, AppDatabase.class, dbPath)
            .addMigrations(RoomDatabaseMigrationProvider.MIGRATION_1_2)
            .build();
}