4

I am trying to access a databse on an Android's internal storage by specifying its path using the Room library. So far, I have been doing it by using the following statement:

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

How do I acces this DB with Room?

Jan Málek
  • 531
  • 8
  • 21
  • 1
    AFAIK you have to implement custom `SupportSQLiteOpenHelper` and `SupportSQLiteOpenHelper.Factory` then use `RoomDatabase.Builder.openHelperFactory` .... `AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").openHelperFactory(customFactory)build();` – Selvin Oct 05 '17 at 11:37
  • First, put Room aside for a bit. Never hardcode paths. That hardcoded value is wrong for many Android devices, based on OS version or user (e.g., secondary accounts). Your hardcoded value maps, more or less, to `new File(new File(getFilesDir(), "database"), "myDataBase.db"))`. But then I agree with Selvin; because you have your database in a non-standard location, AFAIK you will need a custom `SupportSQLiteOpenHelper` that can derive the proper location at runtime. – CommonsWare Oct 05 '17 at 11:49
  • '@CommonsWare You are right and i haven't actualy harcode it, I just wanted to show what such a path might look like. – Jan Málek Oct 05 '17 at 14:32
  • 1
    But, how are we going implement `SupportSQLiteOpenHelper`? As, we need to return `SupportSQLiteDatabase`. Implement `SupportSQLiteOpenHelper` interface, is not same as extending `SQLiteOpenHelper` (which is an abstract class) – Cheok Yan Cheng May 16 '18 at 06:49

2 Answers2

2

As far as I know, import db file to room is currently not OK. But there is another way: How to use Room Persistence Library with pre-populated database?

You can try also this library: RoomAsset

gparyani
  • 1,958
  • 3
  • 26
  • 39
amlwin
  • 4,059
  • 2
  • 15
  • 34
-2

@Jan Malek I use this code to know if I have SD Card and get the path NO SD CARD I then get the path for internal storage

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

Try these few lines of code I believe your path is incorrect in the code you posted if this does not work please advise us

        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);
Vector
  • 3,066
  • 5
  • 27
  • 54
  • @janmalek I do not mind a down vote that said you accepted the answer and by clicking the up button would seem logical to me but I have two down votes and do not understand WHY can anyone explain – Vector Oct 16 '17 at 16:28
  • Your response did not answer my question. I was asking how to connect to an SQLite database with Room library. I was not asking how to aquire the path of such database. – Jan Málek Oct 23 '17 at 11:27