40

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

live-love
  • 48,840
  • 22
  • 240
  • 204
KKSINGLA
  • 1,284
  • 2
  • 10
  • 22

5 Answers5

116

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

LeTadas
  • 3,436
  • 9
  • 33
  • 65
  • 4
    doing the same but no tables found – KKSINGLA May 28 '18 at 16:40
  • 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 – LeTadas May 28 '18 at 16:51
  • 1
    DB browser for sqlite with this same error. File size is 4KB only – KKSINGLA May 28 '18 at 17:51
  • I see, .db-wal file contains all data. – LeTadas May 28 '18 at 18:00
  • 2
    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 – LeTadas May 28 '18 at 18:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171934/discussion-between-kushaal-singla-and-kosas). – KKSINGLA May 28 '18 at 21:36
  • I got the data. I'm just copying the single file. – KKSINGLA May 29 '18 at 05:45
  • I am having the same problem what to do please suggest – Abhishek Dubey Jul 21 '18 at 07:07
  • I downloaded these three files but i have error "invalid file format" in open it , please check [https://stackoverflow.com/questions/56628647/android-room-database-file-is-invalid-file-format-in-db-bowser](this) @kosas – milad salimi Jun 17 '19 at 09:58
  • You saved lives – Amit Jayant Sep 27 '20 at 08:07
30

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

Avinash Jadaun
  • 1,892
  • 17
  • 16
  • I have same problem please check [link](https://stackoverflow.com/questions/56628647/android-room-database-file-is-invalid-file-format-in-db-browser) – milad salimi Jun 17 '19 at 10:41
  • 1
    You may need to rename the files as follows: `your-database-name.db`, `your-database-name.db-shm` and `your-database-name.db-wal` – Mr-IDE Apr 28 '20 at 19:13
10

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

Dagmar
  • 2,968
  • 23
  • 27
  • thank you... my root problem is i'm not copying all 3 files – KKSINGLA Mar 01 '20 at 16:46
  • 1
    @KKSINGLA my solution means you don't need to copy all 3 files, which is a lot easier IMHO – Dagmar Mar 03 '20 at 14:46
  • 1
    Nice, i will try this also – KKSINGLA Mar 04 '20 at 13:45
  • 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. – live-love Mar 14 '21 at 19:18
  • @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. – Dagmar Mar 30 '21 at 16:08
7

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

Wale
  • 1,644
  • 15
  • 31
  • 1
    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. – blueware Jul 09 '20 at 10:55
  • @blueware you're right, my bad. will update that sooner, pls you can help improve answer also. – Wale Jul 10 '20 at 01:50
0

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.

hetsgandhi
  • 726
  • 3
  • 9
  • 25