Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where Room save database Android?

I am using Room library to save data in database.i want to get 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();
        }
    }

it works in simple sqlLite but did not work for ROOM Library ROOM

is there any way can get Database?

Class to create DataBase with help of 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;
    }


}
like image 712
Usman Saeed Avatar asked Jul 08 '17 12:07

Usman Saeed


1 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.

These are not the same.

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

like image 82
CommonsWare Avatar answered Sep 20 '22 13:09

CommonsWare