5

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Gaju Kollur
  • 2,046
  • 5
  • 23
  • 47

1 Answers1

4

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.

hjchin
  • 864
  • 2
  • 8
  • 25