Android: SQLite database created with room shows no tables when opening with sqlte-browser

前端 未结 4 1124
别那么骄傲
别那么骄傲 2021-02-05 08:24

I am using Room Persistence Library 1.1.0. I could find the database file at /data/data//databases/ using Android Studio\'s De

4条回答
  •  北海茫月
    2021-02-05 09:16

    Room database Export and Import Solution

    Im facing same problem in one of my project, i spend two days to resolve this issue.

    Solution

    Don't create multiple instance for Room library. Multiple instance creating all the problems.

    MyApplication

    class MyApplication: Application() 
    {
    
    companion object {
        lateinit var mInstanceDB: AppDatabase
    }
    
    override fun onCreate() {
        super.onCreate()
        mInstanceDB = AppDatabase.getInstance(this)
    }
    }
    

    AppDatabase

    fun getInstance(context: Context): AppDatabase 
    {
    
    if (sInstance == null) {
    
        sInstance = Room.databaseBuilder(context.applicationContext,AppDatabase::class.java, "database").allowMainThreadQueries().build() 
    
                return sInstance!!
    }
    }
    

    Now use this instance in any number of activity or fragment just like that

    {
        var allcustomer = MyApplication.mInstanceDB.customerDao.getAll()
    }
    

    Export and Import use this library

    implementation 'com.ajts.androidmads.sqliteimpex:library:1.0.0'
    

    Github link

提交回复
热议问题