繁体   English   中英

Room 在哪里保存数据库 Android?

[英]Where Room save database Android?

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


}
static final String DATABASE_NAME = "photex_db";

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

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

在这里,您正在尝试从photex_db.db中读取。

这些是不一样的。

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

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

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

我的代码中有一个小错误,在更正后可以正常工作

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

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

File backupDB = new File(backupDBPath);

根据文档getDatabasePath Returns the absolute path on the filesystem where a database created with openOrCreateDatabase(String, int, SQLiteDatabase.CursorFactory) is stored. 在房间的情况下它不工作

//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 其中“它”是从 RoomDatabase 继承的数据库对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM