Android Room database file is empty - .db, .db-shm, .db-wal
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
![KKSINGLA](https://lh5.googleusercontent.com/-UKfugp6KvUM/AAAAAAAAAAI/AAAAAAAAAD4/WoaURytCIMk/photo.jpg?sz=256)
Comments
-
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 about 6 yearsdoing the same but no tables found
-
LeTadas about 6 yearsUse 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 about 6 yearsDB browser for sqlite with this same error. File size is 4KB only
-
LeTadas about 6 yearsI see, .db-wal file contains all data.
-
LeTadas about 6 yearsI 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 about 6 yearsLet us continue this discussion in chat.
-
KKSINGLA about 6 yearsI got the data. I'm just copying the single file.
-
Abhishek Dubey almost 6 yearsI am having the same problem what to do please suggest
-
milad salimi about 5 yearsI downloaded these three files but i have error "invalid file format" in open it , please check [stackoverflow.com/questions/56628647/… @kosas
-
milad salimi about 5 yearsI have same problem please check link
-
KKSINGLA over 4 yearsthank you... my root problem is i'm not copying all 3 files
-
Dagmar over 4 years@KKSINGLA my solution means you don't need to copy all 3 files, which is a lot easier IMHO
-
KKSINGLA over 4 yearsNice, i will try this also
-
Mr-IDE about 4 yearsYou may need to rename the files as follows:
your-database-name.db
,your-database-name.db-shm
andyour-database-name.db-wal
-
blueware almost 4 yearsOfc 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 almost 4 years@blueware you're right, my bad. will update that sooner, pls you can help improve answer also.
-
Amit Jayant over 3 yearsYou saved lives
-
live-love over 3 yearsNot 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 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.