房间保存数据库Android?

Usm*_*eed 15 database android android-room

我正在使用Room库来保存数据库中的数据.我想获取数据库.

使用此代码

  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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

有没有办法可以获得数据库?

在Room`的帮助下创建DataBase的类

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


}
Run Code Online (Sandbox Code Playgroud)

}

Com*_*are 16

static final String DATABASE_NAME = "photex_db";
Run Code Online (Sandbox Code Playgroud)

在这里,你正试图打开photoex_db.

String currentDBPath=getDatabasePath("photex_db.db").getAbsolutePath();
Run Code Online (Sandbox Code Playgroud)

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

这些都不一样.

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

  • @MayurRokade:`getDatabasePath()`是`Context`的一个方法. (5认同)
  • 我应该在哪里调用`getDatabasePath()`。我是说哪个班级提供的? (2认同)

Ale*_*Pad 8

在 Android Studio 中,右下角有一个名为“设备文件资源管理器”的部分。在本节中,您可以浏览所有文件(在简单的文件浏览器应用程序中看不到这些文件,因为您需要运行根目录)。

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


Usm*_*eed 5

在纠正其工作正常后,我的代码中出现了一点错误

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();
    }
}
Run Code Online (Sandbox Code Playgroud)