19

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;
    }


}
mohammadReza Abiri
  • 1,759
  • 1
  • 9
  • 20
Usman Saeed
  • 843
  • 3
  • 9
  • 21
  • 1
    "it works in simple sqlLite" -- not on many devices or for many users. **Never hardcode paths**. [Use `getDatabasePath()`, please](https://developer.android.com/reference/android/content/Context.html#getDatabasePath(java.lang.String)). "did not work for ROOM Library" -- if you use `getDatabasePath()`, it should work with Room. If you encounter problems, provide a [mcve], which would include the `RoomDatabase` where you are providing the information to Room about what database to use. – CommonsWare Jul 08 '17 at 13:01
  • @CommonsWare i tried getDatabasePath() it does not help :( – Usman Saeed Jul 10 '17 at 12:25
  • 1
    Then, as I wrote, please provide a [mcve], which would include the `RoomDatabase` where you are providing the information to Room about what database to use. – CommonsWare Jul 10 '17 at 12:41
  • I updated the question ,see if it is this you mean – Usman Saeed Jul 10 '17 at 12:52
  • You can check [this](https://stackoverflow.com/questions/44925671/why-is-there-no-database-file-created-when-using-android-room) answer. Maybe you just create database incorrectly. – WorieN Jul 10 '17 at 14:01

6 Answers6

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
15

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.

AlexPad
  • 10,364
  • 3
  • 38
  • 48
5

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();
    }
}
Usman Saeed
  • 843
  • 3
  • 9
  • 21
  • getDatabasePath("photex_db") returns file object, File currentDB = getDatabasePath("photex_db") for simplicity – hjchin Jul 04 '18 at 14:58
  • To Get the Room database storage path using SupportSQLiteOpenHelper refer link : https://stackoverflow.com/a/57069135/5453198 – Sackurise Jul 18 '19 at 05:20
3

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);
Prashant
  • 1,046
  • 14
  • 21
0

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

Manan
  • 394
  • 4
  • 15
0
//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

alexis
  • 31
  • 5