【问题标题】:Where Room save database Android?Room保存数据库Android在哪里?
【发布时间】:2017-07-08 12:56:19
【问题描述】:

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


}

【问题讨论】:

  • “它在简单的 sqlLite 中工作”——不适用于许多设备或许多用户。 从不硬编码路径Use getDatabasePath(), please。 “不适用于 ROOM 库”——如果您使用 getDatabasePath(),它应该适用于 Room。如果您遇到问题,请提供minimal reproducible example,其中将包括您向 Room 提供有关使用什么数据库的信息的RoomDatabase
  • @CommonsWare 我试过 getDatabasePath() 它没有帮助:(
  • 然后,正如我所写,请提供minimal reproducible example,其中包括RoomDatabase,您将在其中向 Room 提供有关使用什么数据库的信息。
  • 我更新了问题,看看是不是你的意思
  • 你可以查看this的答案。也许你只是错误地创建了数据库。

标签: android database android-room


【解决方案1】:
static final String DATABASE_NAME = "photex_db";

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

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

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

这些不一样。

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

【讨论】:

  • 我应该在哪里打电话给getDatabasePath()。我的意思是哪个类提供它?
  • @MayurRokade: getDatabasePath()Context 上的一个方法。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    我的代码有一个小错误,纠正它的工作正常后

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

    【讨论】:

    • getDatabasePath("photex_db") 返回文件对象,为简单起见 File currentDB = getDatabasePath("photex_db")
    • 使用 SupportSQLiteOpenHelper 获取 Room 数据库存储路径参考链接:stackoverflow.com/a/57069135/5453198
    【解决方案4】:

    试试这个代码:

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

    它将返回您的数据库的路径。使用此确切路径创建要复制的文件。它肯定对我有用。

    File backupDB = new File(backupDBPath);
    

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 只是想指出我正在使用 Room (2.3.0) 并且 getDatabasePath() 对我来说工作正常..
      【解决方案6】:
      //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)
          }
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-06
        • 1970-01-01
        • 2010-11-09
        • 2012-07-07
        • 1970-01-01
        相关资源
        最近更新 更多