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

18,522

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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

Share:
18,522
KKSINGLA
Author by

KKSINGLA

Open to Challenges Developing Android Apps and ___.

Updated on June 14, 2022

Comments

  • KKSINGLA
    KKSINGLA about 2 years

    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

  • KKSINGLA
    KKSINGLA about 6 years
    doing the same but no tables found
  • LeTadas
    LeTadas about 6 years
    Use DB browser for sqlite, it's most likely because your editor doesn't support new sql functions which was released with new room version
  • KKSINGLA
    KKSINGLA about 6 years
    DB browser for sqlite with this same error. File size is 4KB only
  • LeTadas
    LeTadas about 6 years
    I see, .db-wal file contains all data.
  • LeTadas
    LeTadas about 6 years
    I tried now as well so it seems some sort of bug in android studio when saving files, because it some times work some times it shows empty database. What I did saved all three files opened .db file it was empty so I closed db editor deleted all files saved again and opened then it was with all data
  • KKSINGLA
    KKSINGLA about 6 years
  • KKSINGLA
    KKSINGLA about 6 years
    I got the data. I'm just copying the single file.
  • Abhishek Dubey
    Abhishek Dubey almost 6 years
    I am having the same problem what to do please suggest
  • milad salimi
    milad salimi about 5 years
    I downloaded these three files but i have error "invalid file format" in open it , please check [stackoverflow.com/questions/56628647/… @kosas
  • milad salimi
    milad salimi about 5 years
    I have same problem please check link
  • KKSINGLA
    KKSINGLA over 4 years
    thank you... my root problem is i'm not copying all 3 files
  • Dagmar
    Dagmar over 4 years
    @KKSINGLA my solution means you don't need to copy all 3 files, which is a lot easier IMHO
  • KKSINGLA
    KKSINGLA over 4 years
    Nice, i will try this also
  • Mr-IDE
    Mr-IDE about 4 years
    You may need to rename the files as follows: your-database-name.db, your-database-name.db-shm and your-database-name.db-wal
  • blueware
    blueware almost 4 years
    Ofc it works with you, not by a chance but because the other database files are needed to open the original database file. Please provide more accurate answer and why it should work.
  • Wale
    Wale almost 4 years
    @blueware you're right, my bad. will update that sooner, pls you can help improve answer also.
  • Amit Jayant
    Amit Jayant over 3 years
    You saved lives
  • live-love
    live-love over 3 years
    Not guaranteed onDestroy will be called in your activity, and these files are used by Room, so they are always there, cannot get rid of them, unless you set WriteAhead.
  • Dagmar
    Dagmar about 3 years
    @live-love you have to understand the context of the question and my answer. It certainly is possible to make Sqlite finalise all the outstanding transactions and thus move all the data from the wal file into the main database file. This is helpful if you want to export your data off the device and view it on your computer. However you are correct - onDestroy is not guaranteed, but if you are in control you can make sure it is.