使用Room

时间:2017-10-05 11:23:19

标签: android sqlite android-sqlite android-room

我试图通过使用Room库指定其路径来访问Android内部存储上的数据库。到目前为止,我一直在使用以下语句来完成它:

dbPath = "/data/data/my.sqlitetesting/files/database/myDataBase.db";
SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);

如何使用Room访问此数据库?

2 个答案:

答案 0 :(得分:2)

据我所知,导入db文件到房间目前还不行。但还有另一种方式:How to use Room Persistence Library with pre-populated database?

您也可以尝试使用此库:RoomAsset

答案 1 :(得分:-2)

@Jan Malek我用这段代码知道我是否有SD卡并获得路径没有SD卡 然后我获得了内部存储的路径

    public void onAvail() {

    String state = Environment.getExternalStorageState();

    if (state.equals(Environment.MEDIA_MOUNTED) && (!state.equals(Environment.MEDIA_MOUNTED_READ_ONLY))) {

        File removable = ContextCompat.getExternalFilesDirs(this, null)[1];
        THE_PATH = String.valueOf(removable) + "/Documents/";

        //System.out.println("ALL TRUE ==> " + THE_PATH);

    } else {// if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        THE_PATH = "";
        //System.out.println("ALL FALSE ==> "+ THE_PATH);
    }
}

尝试以下几行代码我相信您发布的代码中的路径不正确如果不起作用请告知我们

        Context context = this;
    String removable = getFilesDir().getAbsolutePath();
    THE_PATH = String.valueOf(removable)+"/";
    System.out.println("INTERNAL PATH ======> "+THE_PATH);
    File file = new File(context.getFilesDir(), "NAME_OF_FILE");
    String PA = String.valueOf(file);
    System.out.println("Where AM  I ======> "+PA);