如何在Room databasebuilder中使用CreateFromAsset

问题描述 投票:0回答:1

我正在尝试使此新功能正常运行,但是我一直在获取一个空数据库。没有太多的文档,媒体上的文章还不够详细。

使用debugImplementation com.amitshekhar.android:debug-db:1.0.6插件查看我的数据库并手动向其中添加一个条目。

这样做之后,我下载了数据库。

该文件本身不是*.db文件,但我更改了名称并放入.db扩展名(我尝试过使用和不使用)。

我将其添加到我的资产文件夹,并在我的数据库生成器上添加了一个.createFromAsset及其路径,以便可以使用该数据库。

重新启动应用程序(删除并重新安装)时,我发现它不是预先填充的。

我到底在做什么错?

这就是我创建数据库的方式。没有什么特别的。

single(createdAtStart = true) {
    Room.databaseBuilder(
        get(),
        CountryDatabase::class.java,
        "country_database"
    ).createFromAsset("database/country_database.db").build()
}

single(createdAtStart = true) {
    get<CountryDatabase>().countryDao()
}

@Database(entities = [LocalCountryData::class], version = 4)
abstract class CountryDatabase : RoomDatabase() {
    abstract fun countryDao(): CountryDao
}

@Entity(tableName = "countries")
data class LocalCountryData(
    @PrimaryKey
    val country_name: String,
    val country_short_code: String,
    val regions_name: String,
    val regions_short_code: String
)
android database sqlite retrofit android-room
1个回答
0
投票

重新启动应用程序时,我发现它不是预先填充的。

如果没有现有数据库,将仅调用

createFromAssets并从assets文件夹的database文件夹复制数据库。

而不是重新启动应用程序,您应该:-

  • 删除应用程序的数据或
  • 卸载应用程序

然后运行应用程序。

© www.soinside.com 2019 - 2024. All rights reserved.