android - android房间迁移不调用
问题描述
在我的应用程序中,我需要迁移来创建一个新表并将数据插入其中。表本身已创建但那里没有数据,我试图破坏迁移调用方法,但它没有被调用。为什么不叫迁移?
应用程序.kt
override fun onCreate() {
super.onCreate()
instance = this
database = Room.databaseBuilder(applicationContext, TramDatabase::class.java, "tram_database")
.addMigrations(Migrations.MIGRATION_1_2)
.addCallback(dbCallback)
.allowMainThreadQueries()
.build()
Stetho.initializeWithDefaults(this)
}
迁移.kt
class Migrations {
companion object {
val MIGRATION_1_2: Migration = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE IF NOT EXISTS ${TrackPoint.TABLE_NAME} " +
"(${TrackPoint.Columns.ID} INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
"${TrackPoint.Columns.NUMBER} TEXT NOT NULL, " +
"${TrackPoint.Columns.LONGITUDE} DOUBLE PRECISION NOT NULL, " +
"${TrackPoint.Columns.LATITUDE} DOUBLE PRECISION NOT NULL, " +
"${TrackPoint.Columns.ORDER} INTEGER NOT NULL)")
Timber.d("rrrrrrrrrrr")
val filename = "track_points.sql"
try {
val input = App.instance.assets.open(filename)
val output = ByteArrayOutputStream()
val buffer = ByteArray(1024)
var length = input.read(buffer)
while (length != -1) {
output.write(buffer, 0, length)
length = input.read(buffer)
}
val insertSQl = output.toString("UTF-8")
input.close()
output.close()
try {
database.beginTransaction()
database.execSQL(insertSQl)
database.setTransactionSuccessful()
} finally {
database.endTransaction()
}
} catch (error: Exception) {
Timber.e(error)
}
}
}
}
电车数据库.kt
@Database(entities = [MapRoute::class, TrackPoint::class], version = 2)
abstract class TramDatabase: RoomDatabase() {
abstract fun mapRouteDao() : MapRouteDao
}
解决方案
推荐阅读
- python - Python将日期元组字符串转换为元组列表
- javascript - 错误的结果使用复选框使用 Jquery 计算值
- android - Android - 应用程序在 Chrome 启动时启动 - 这怎么可能?
- kotlin - “将属性获取器转换为初始化器” - 但它们不是一回事,是吗?
- python - 通过拟合python中的数据绘制指数曲线
- google-cloud-platform - 我的 GCE 实例中的那些“gke-xxxx”用户是什么
- c++ - 如何使用 opencv 的 VideoCapture 连接 Gopro cam?
- c++ - 使用具有动态分配数组的迭代器时出现问题
- python - 对于每一行,返回最小值的列名 - pandas
- visual-studio - Visual Studio CPU Usage 中两条相似汇编指令的性能差异