Room 保存数据库在哪里Android?

Where Room save database Android?

我正在使用 Room 库将数据保存在 database.i 想要获取数据库。

使用了这个代码

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

它适用于简单的 sqlLite,但不适用于 ROOM 库 ROOM

有什么办法可以得到数据库吗?

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


}
static final String DATABASE_NAME = "photex_db";

在这里,您正在尝试打开 photoex_db

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

在这里,您正在尝试阅读 photex_db.db

这些不一样。

您可以考虑始终如一地使用 DATABASE_NAME,而不是只在某些地方使用。

我的代码有一点点错误,改正后可以正常工作

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

试试这个代码:

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

它将return 数据库的路径。使用此确切路径在要复制的位置创建文件。它肯定会像我一样工作。

File backupDB = new File(backupDBPath);

在 Android Studio 的右下角有一个名为 "Device File Explorer" 的部分。在本节中,您可以浏览所有文件 (您在简单的文件浏览器应用程序中看不到,因为您需要 运行 根目录)。

确保模拟器是您正在使用的模拟器。 在此资源管理器中,您必须转到 "data" -> "data",查找您的应用程序的包名称,下一步是找到 "database" 条目,在此文件夹中有您的房间数据库。

根据 doc getDatabasePath Returns the absolute path on the filesystem where a database created with openOrCreateDatabase(String, int, SQLiteDatabase.CursorFactory) is stored. 如果 Room 不工作

//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)
    }

关注it.openHelper.writableDatabase.path 其中“it”是您从 RoomDatabase

继承的 db 对象