Android Room database file is empty

前端 未结 5 823
一生所求
一生所求 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//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

提交回复
热议问题