Room 2.5.0-alpha02
I need the prepopulate one table data to the database , So I make a .db
file only have one
table which I need inflate the prepopulate data , The Android developer document
told 2.4.0 +
Room
have auto migrate ,and So I not write the migrate code . But when I build and run app, the data not add to the app database.
DataBase.kt
I add the fallbackToDestructiveMigration , and the app db version is higher one than the .db
file
@Database(
entities = [DetectAd::class, DetectResult::class, DetectItem::class],
exportSchema = false,
version = 2
)
@TypeConverters(Converter::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun detectItemDao(): DetectItemDao
companion object {
@Volatile
private var INSTANCE: AppDatabase? = null
fun getDatabase(context: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"app_db.db"
)
.createFromAsset("database/appDatabase2.db")
.fallbackToDestructiveMigration()
.build()
INSTANCE = instance
// return instance
instance
}
}
}
appDatabase2.db file
App db after run
The prepopulate data not inflate to the db
So, should I make a complete prepopulate .db
file which struct 100% same like the app db struct, although I only want to prepoulate one table data? If not ,what i need do ?
Thanks for your help.
Additional remarks
When I make the app db version to one (same to file) , app hava a error tip:
java.lang.IllegalStateException: Pre-packaged database has an invalid schema: detectAd(com.sunbio.coagulation.data.db.entity.DetectAd).
Expected:
TableInfo{name='detectAd', columns={Ad4=Column{name='Ad4', type='TEXT', affinity='2', notNull=notNull, primaryKeyPosition=0, defaultValue='null'}, Ad3=Column{name='Ad3', type='TEXT', affinity='2', notNull=notNull, primaryKeyPosition=0, defaultValue='null'}, Ad6=Column{name='Ad6', type='TEXT', affinity='2', notNull=notNull, primaryKeyPosition=0, defaultValue='null'}, Ad5=Column{name='Ad5', type='TEXT', affinity='2', notNull=notNull, primaryKeyPosition=0, defaultValue='null'}, Ad8=Column{name='Ad8', type='TEXT', affinity='2', notNull=notNull, primaryKeyPosition=0, defaultValue='null'}, Ad7=Column{name='Ad7', type='TEXT', affinity='2', notNull=notNull, primaryKeyPosition=0, defaultValue='null'}, adNo=Column{name='adNo', type='INTEGER', affinity='3', notNull=notNull, primaryKeyPosition=1, defaultValue='null'}, sampleNo=Column{name='sampleNo', type='TEXT', affinity='2', notNull=notNull, primaryKeyPosition=0, defaultValue='null'}, time=Column{name='time', type='TEXT', affinity='2', notNull=notNull, primaryKeyPosition=0, defaultValue='null'}, Ad2=Column{name='Ad2', type='TEXT', affinity='2', notNull=notNull, primaryKeyPosition=0, defaultValue='null'}, Ad1=Column{name='Ad1', type='TEXT', affinity='2', notNull=notNull, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='detectAd', columns={}, foreignKeys=[], indices=[]}
at androidx.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.kt:159)
at androidx.room.RoomOpenHelper.onOpen(RoomOpenHelper.kt:128)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(FrameworkSQLiteOpenHelper.java:326)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:409)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableOrReadableDatabase(FrameworkSQLiteOpenHelper.java:273)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.innerGetDatabase(FrameworkSQLiteOpenHelper.java:225)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getSupportDatabase(FrameworkSQLiteOpenHelper.java:183)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:133)
at androidx.room.SQLiteCopyOpenHelper.getWritableDatabase(SQLiteCopyOpenHelper.kt:71)
at androidx.room.RoomDatabase.inTransaction(RoomDatabase.kt:634)
at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.kt:430)
at com.sunbio.coagulation.data.db.dao.DetectItemDao_Impl.getAllName(DetectItemDao_Impl.java:270)
at com.sunbio.coagulation.ui.manager.DetectManagerFragment$getData$1$1.invokeSuspend(DetectManagerFragment.kt:196)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)