如何迁移会议室数据库?

时间:2020-06-24 12:15:17

标签: java android kotlin android-room database-migration

我是新来者。 如何迁移会议室数据库。 我尝试使用此说明https://developer.android.com/training/data-storage/room/migrating-db-versions进行迁移,但不知道应在哪里使用它。 我将版本从1更改为2,并添加了列 @ColumnInfo(name =“ age”)val age:Int

您能帮我迁移它吗?

@Entity
data class User(
    @PrimaryKey(autoGenerate = true) val uid: Int = 0,
    @ColumnInfo(name = "first_name") val firstName: String?,
    @ColumnInfo(name = "last_name") val lastName: String?,
    @ColumnInfo(name = "age") val age: Int
)

@Dao
interface UserDao {
@Query("SELECT * FROM user")
fun getAll(): LiveData<List<User>>

@Query("SELECT * FROM user WHERE uid IN (:userIds)")
fun loadAllByIds(userIds: IntArray): List<User>

@Query(
    "SELECT * FROM user WHERE first_name LIKE :first AND " +
            "last_name LIKE :last LIMIT 1"
)
fun findByName(first: String, last: String): User

@Insert
fun insertAll(vararg users: User)

@Delete
fun delete(user: User)
}

@Database(entities = arrayOf(User::class), version = 2)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao

companion object {
    private val mBD = AppDatabase

    val MIGRATION_1_2 = object : Migration(1, 2) {
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("ALTER TABLE 'User' ADD COLUMN 'age' INTEGER DEFAULT 0")
        }
    }

}

}

0 个答案:

没有答案