Android Room database file is empty

前端 未结 5 814
一生所求
一生所求 2020-12-13 06:38

Using room in android for database. When I tried to see the data in sqlviewer then no tables found in database file Myapp.db file is empty. Data/data/packageName/databases/M

相关标签:
5条回答
  • 2020-12-13 07:18

    Make sure your database is closed when you exit your app, which will ensure that all outstanding transactions are committed:

    @Override
    protected void onDestroy() {
        super.onDestroy();
    
        // close the primary database to ensure all the transactions are merged
        MyAppDatabase.getInstance(getApplicationContext()).close();
    }
    

    Then you can copy the database off your device using the Device File Explorer in Android Studio (look under /data/data/<your app package name>/databases).

    You are also able to force a WAL checkpoint instead of closing the database, but in my humble opinion this option is easier (unless you are trying to backup your database within the app programmatically or something like that) and closing databases is a good idea (even if it's not really mentioned in the Android Room documentation).

    Read more about sqlite WAL here: https://www.sqlite.org/wal.html

    0 讨论(0)
  • 2020-12-13 07:19

    Go to folder Data/data/packageName/databases/ There has to be three files .db, .db-shm, .db-wal, copy all three files to one location and then open your Myapp.db these two extra files are needed to open db file

    0 讨论(0)
  • 2020-12-13 07:21

    Once check your Database class,

    @Database(entities = arrayOf(Test::class), version = 1, exportSchema = false)
    
    abstract class MyDatabase:RoomDatabase() {
           private var INSTANCE: MyDatabase? = null
           abstract fun testDao(): TestDao
    }
    

    Here, MyDatabase is my Database class, Test is the table name and TestDao is the Dao class.

    0 讨论(0)
  • 2020-12-13 07:27

    in android studio 3.1.*

    in tool window bar click on "Device File explorer" generally you can find this in bottom right corner of the screenn

    open directory in data/data/your-application-package/databases

    with new architecture 3 files is created in databases directory

    your-database-name
    your-database-name-shm
    your-database-name-wal
    

    you have to export all 3 in the same directory

    then open first one file (that is with your-database-name only ) in any sqlite browser.

    and now you can see all your data .......

    your-database-name-shm
    your-database-name-wal
    

    these two extra files are needed to open db file if you will open database file only, than you will not found any table in that file

    0 讨论(0)
  • 2020-12-13 07:34

    I saved the whole folder and that's it, I could browse the table.

    0 讨论(0)
提交回复
热议问题