Where Room save database Android?

后端 未结 6 1872
借酒劲吻你
借酒劲吻你 2020-12-30 02:39

I am using Room library to save data in database.i want to get database.

used this code

  private void copyFile() {

        try {
            File s         


        
相关标签:
6条回答
  • 2020-12-30 03:16

    There was a little mistake in my code ,after correcting its working fine

    private void copyFile() {
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
    
            if (sd.canWrite()) {
                String currentDBPath =
                        getDatabasePath("photex_db").getAbsolutePath();
                String backupDBPath = "photex_db.db";
                //previous wrong  code  
                // **File currentDB = new File(data,currentDBPath);**
                // correct code
                File currentDB = new File(currentDBPath);
                File backupDB = new File(sd, backupDBPath);
    
                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    0 讨论(0)
  • 2020-12-30 03:20
    //kotlin
    Room.databaseBuilder(context, AppDatabase::class.java, "appData.sqlite")
        .addMigrations(
            MIGRATION_1_2,
            MIGRATION_2_3
        )
        .build()
        .also {
            Log.d("<DEV>", it.openHelper.writableDatabase.path)
        }
    

    focus on it.openHelper.writableDatabase.path where "it" is your db object inherited from RoomDatabase

    0 讨论(0)
  • 2020-12-30 03:25

    in Android Studio there is a section called "Device File Explorer", at the bottom right. In this section you can explore all the files (which you can not see in a simple file explorer app because you need to run the root).

    Make sure the emulator is the one you're working with. In this explorer you have to go to "data" -> "data", look for the package name of your app and the next step is to find "database" entry, in this folder there is your Room database.

    0 讨论(0)
  • 2020-12-30 03:25

    According to doc getDatabasePath Returns the absolute path on the filesystem where a database created with openOrCreateDatabase(String, int, SQLiteDatabase.CursorFactory) is stored. In case of Room it is not working

    0 讨论(0)
  • 2020-12-30 03:33

    Try out this code :

    String backupDBPath = YourRoomDatabase.getDatabase(context).getOpenHelper().getWritableDatabase().getPath();
    

    It will return the path of your database. Use this exact path to create the file where you want to copy it. It will definitely work as worked for me.

    File backupDB = new File(backupDBPath);
    
    0 讨论(0)
  • 2020-12-30 03:35
    static final String DATABASE_NAME = "photex_db";
    

    Here, you are trying to open photoex_db.

    String currentDBPath=getDatabasePath("photex_db.db").getAbsolutePath();
    

    Here, you are trying to read from photex_db.db.

    These are not the same.

    You might consider using DATABASE_NAME consistently, rather than only in some places.

    0 讨论(0)
提交回复
热议问题