预播室数据库不起作用,但是在Kotlin中创建了数据库

时间:2020-03-24 09:49:06

标签: kotlin android-room

我正在尝试使用Room Database在Kotlin中预植入我的数据库。构建并运行它并创建数据库但它没有种子时,它不会给我任何错误。

这是我的代码:

FareMatrix.kt

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "fare_matrix")
data class FareMatrix(
    @PrimaryKey
    var id: Int = 0,
    val entry_plaza: Int? = null,
    val exit_plaza: Int? = null,
    val classification: Int? = null,
    val amt_total: Int? = null,
    val vat: Double? = null,
    val payment_mode: Int? = null
)

fareMatrixDao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query

@Dao
interface FareMatrixDao {
    @Insert
    fun addFareMatrix(fareMatrix: List<FareMatrix>)
}

FareMatrixDatabase

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase

@Database(
    entities = [FareMatrix::class],
    version = 9
)
abstract class LcsDatabase: RoomDatabase() {

    abstract fun getFareMatrixDao(): FareMatrixDao

    companion object {
        @Volatile private var instance: LcsDatabase? = null
        private val lock = Any()

        operator fun invoke(context: Context) = instance ?: synchronized(lock){
            instance ?: buildDatabase(context).also {
                instance = it
            }
        }

        private fun buildDatabase(context: Context) = Room.databaseBuilder(
            context.applicationContext,
            LcsDatabase::class.java,
            "lcsdatabase.db"
        ).fallbackToDestructiveMigration()
            .addCallback(object: Callback() {
                override fun onCreate(db: SupportSQLiteDatabase) {
                    super.onCreate(db)

                    Thread(Runnable {
                        seeDatabase(context)
                        println("END")
                    }).start()
                }
            })
            .build()

        fun seeDatabase(context: Context) {
            val POPULATED_FAREMATRIX_DATA = listOf<FareMatrix>(
                FareMatrix(1,800,803,1,30,3.21,1),
                FareMatrix(2,800,803,2,75,8.04,1),
                FareMatrix(3,800,803,3,90,9.64,1),
                FareMatrix(4,800,805,1,58,6.21,1),
                FareMatrix(5,800,805,2,145,15.54,1)
            )
            val db = invoke(context)
            var fmDao = db.getFareMatrixDao()
            fmDao.addFareMatrix(POPULATED_FAREMATRIX_DATA)
        }
    }
}

我什至尝试使用这种方式:

fun seeDatabase(context: Context) {
            val POPULATED_FAREMATRIX_DATA = listOf<FareMatrix>(
                FareMatrix(1,800,803,1,30,3.21,1),
                FareMatrix(2,800,803,2,75,8.04,1),
                FareMatrix(3,800,803,3,90,9.64,1),
                FareMatrix(4,800,805,1,58,6.21,1),
                FareMatrix(5,800,805,2,145,15.54,1)
            )
            val db = invoke(context)
            var fmDao = db.getFareMatrixDao()
            for (i in POPULATED_FAREMATRIX_DATA) {
                fmDao.addFareMatrix(FareMatrix(i.id, i.entry_plaza, i.exit_plaza, i.classification, i.amt_total, i.vat, i.payment_mode))
            }
        }

DAO

 @Insert
    fun addFareMatrix(fareMatrix: FareMatrix)

如何使它正常工作?

1 个答案:

答案 0 :(得分:2)

也许您可以尝试Kotlin协程。到目前为止,您的代码是好的,但是在数据插入期间可能格式不正确。因此,我建议您使用kotlinx.coroutines并在其末尾的invoke方法中使用

之类的方法。
   operator fun invoke(context: Context) = instance ?: synchronized(LOCK) {
      instance ?: buildDatabase(context).also {
            instance = it

      GlobalScope.launch {
            val POPULATED_FAREMATRIX_DATA = listOf<FareMatrix>(
            FareMatrix(1,800,803,1,30,3.21,1),
            FareMatrix(2,800,803,2,75,8.04,1),
            FareMatrix(3,800,803,3,90,9.64,1),
            FareMatrix(4,800,805,1,58,6.21,1),
            FareMatrix(5,800,805,2,145,15.54,1)
        )
           POPULATED_FAREMATRIX_DATA.forEach { something -> it.getFareMatrixDao().addFareMatrix(something) }
     }
 }}

这应该像一种魅力。其他一切看起来都很好。

编辑:您的建筑数据库方法应以 .fallbackToDestructiveMigration()。build()。您无需添加其他任何内容