Where to save the Android database?

I use the Room library to store data in a database. I want to get a database.

used this code

  private void copyFile() {

        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String currentDBPath=getDatabasePath("photex_db.db").getAbsolutePath();
                String backupDBPath = "photex_db.db";
                File currentDB = new File(data, 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();
        }
    }

this works in plain sqlLite but doesn't work for ROOM Library ROOM

Is there any way to get the database?

Class for creating a database using a room

  @Database(entities = {ProjectDataEntity.class, SavedProjectEntity.class},
        version = 2)
     @TypeConverters(DateConverter.class)

     public abstract class AppDatabase extends RoomDatabase {

     static final String DATABASE_NAME = "photex_db";

     private static AppDatabase Instance;

     public abstract ProjectDataDao projectDataDao();

     public abstract SavedProjectDao savedProjectDao();

     public static AppDatabase getInstance(Context context) {
        if (Instance == null) {
            synchronized (AppDatabase.class) {
                if (Instance == null) {
                    Instance = 
      Room.databaseBuilder(context.getApplicationContext(),
                            AppDatabase.class, DATABASE_NAME)
                            .build();
                }
            }
        }
        return Instance;
    }


}
+14
source share
4 answers
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.

This is not the same thing.

You can use it DATABASE_NAMEconsistently, and not just in some places.

+14
source

,

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();
    }
}
+5

Android Studio "Device File Explorer". ( , root).

, . "data" → "data", , - "database", Room.

+1

:

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

It will return the path to your database. Use this exact path to create the file to which you want to copy it. It will definitely work like mine.

File backupDB = new File(backupDBPath);
0
source

Source: https://habr.com/ru/post/1681013/


All Articles