KKSINGLA
KKSINGLA

Reputation: 1344

Android Room database file is empty - .db, .db-shm, .db-wal

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/Myapp.db

Upvotes: 40

Views: 21595

Answers (5)

Dagmar
Dagmar

Reputation: 3261

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

Upvotes: 10

Wale
Wale

Reputation: 1796

I saved the whole folder and that's it, I could browse the table.enter image description here

Upvotes: 7

Avinash Jadaun
Avinash Jadaun

Reputation: 1912

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

Upvotes: 30

hetsgandhi
hetsgandhi

Reputation: 806

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.

Upvotes: 0

LeTadas
LeTadas

Reputation: 3556

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

Upvotes: 117

Related Questions