Android Room Library 无法从 Asset 复制数据库 [英] Android Room Library fails to copy database from Asset

查看:37
本文介绍了Android Room Library 无法从 Asset 复制数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从资产中复制 SQLite 数据库,但它没有复制它也没有抛出任何异常

I want to copy SQLite database from asset but it is not copying it is not throwing any Exception also

@Database(entities = [UserDetails::class, CircleMaster::class], version = 1, exportSchema = false

abstract class AppDatabase : RoomDatabase() {abstract fun getLoginDao(): LoginDao

abstract fun getRegisterDao(): RegisterDao


companion object {
    @Volatile
    private var instance: AppDatabase? = 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,
        AppDatabase::class.java,
        "Asset.db"
    ).createFromAsset("database/Asset.db").allowMainThreadQueries().build()
}}

推荐答案

其实我从这个链接得到了答案

Actually I got an answer from this link

房间:未创建数据库

当我试图插入数据时,从资产复制数据库并插入数据有些人会遇到迁移异常,我通过从资产数据库中删除房间主表来解决这个问题.

when I tried to insert data that time database is copied from asset and data is inserted some people will face migration exception, I solved that by deleting room master table from the asset database.

在幕后,默认情况下,Room 使用 SQLiteOpenHelper,就像您直接使用它一样.

Under the covers, by default, Room uses SQLiteOpenHelper, much as you might use it directly.

SQLiteOpenHelper 在您创建 SQLiteOpenHelper 实例时不会创建数据库.一旦您调用 getReadableDatabase()getWriteableDatabase(),它就会这样做.

SQLiteOpenHelper does not create the database when you create the SQLiteOpenHelper instance. It will do so once you call getReadableDatabase() or getWriteableDatabase().

从 Room 的角度来看,这意味着在您执行一些具体操作之前,例如调用命中数据库的 @Dao 方法,您的数据库将不会被创建.

From a Room standpoint, that means until you perform some concrete operation, such as invoking a @Dao method that hits the database, your database will not be created.

这篇关于Android Room Library 无法从 Asset 复制数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆