18

我正在使用 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();
        }
    }

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

有什么办法可以获取数据库吗?

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


}
4

6 回答 6

18
static final String DATABASE_NAME = "photex_db";

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

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

在这里,您尝试从photex_db.db.

这些不一样。

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

于 2017-07-10T12:54:05.027 回答
11

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

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

于 2018-10-24T10:11:55.843 回答
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();
    }
}
于 2017-07-11T07:01:25.137 回答
2

试试这个代码:

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

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

File backupDB = new File(backupDBPath);
于 2018-04-28T14:57:39.760 回答
0

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

于 2020-05-26T16:19:21.460 回答
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)
    }

关注 it.openHelper.writableDatabase.path 其中“it”是从 RoomDatabase 继承的 db 对象

于 2020-07-15T10:23:53.483 回答