Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view the tables of Room Database in sqlite DB browser?

I am using RoomDB in my sample app.i am able to perform CRUD operation on DB. but i am not able to view db file.i want to view table structure in sqlite DB browser.

this is my APPDatabase class

 @Database(entities = {User.class}, version = 1,exportSchema = false)

public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase INSTANCE;  
    public abstract UserDao userDao();        
    public static AppDatabase getAppDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "user-database")                                
                            .allowMainThreadQueries()
                            .build();
        }
        return INSTANCE;
    }    
    public static void destroyInstance() {
        INSTANCE = null;
    }
}

help me to get DB file.

like image 715
Gaju Kollur Avatar asked Feb 21 '18 07:02

Gaju Kollur


People also ask

How do I View data in DB browser SQLite?

To open the database in DB Browser do the following; Click on the 'open database' button in the toolbar. Navigate to where you have stored the database file on your local machine, select it and click open.

How do I View room DB?

Go To App Inspection . Then select the process. Database Inspector will now show the database contents. in 2021, with current Android Studio, this should be the only accepted answer.

What is the difference between SQLite and room database?

Room vs SQLiteRoom provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite. In the case of SQLite, There is no compile-time verification of raw SQLite queries. But in Room, there is SQL validation at compile time.


1 Answers

This is how I have done it. In Android Studio 3.1,

  1. Access to View>Tool Windows>Device File Explorer.
  2. Navigate to folder /data/data/<your package>/databases
  3. Save your database file which ended with .db

If you can't see your database file, consider to copy the file with following code

fun exportDB(context: Context){
        try {
            val sd = Environment.getExternalStorageDirectory()

            if (sd.canWrite()) {
                val backupDBPath = "database.db"
                val currentDB = context.getDatabasePath("database.db")
                val backupDB = File(sd, backupDBPath)

                val src = FileInputStream(currentDB).getChannel()
                val dst = FileOutputStream(backupDB).getChannel()
                dst.transferFrom(src, 0, src.size())
                src.close()
                dst.close()
                Log.i("database","export database done. Original path: $currentDB")
            }else{
                Log.i("database","cannot write database")
            }
        } catch (e: Exception) {
            Log.e("database","export database failed. "+e.message)
        }
    }

UPDATE: the db file is empty, and u need .db-shm and .db-wal to open .db file.

like image 65
hjchin Avatar answered Oct 19 '22 02:10

hjchin