2

"The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite."

Room Persistence Library Guide

So I came up with this method

public static boolean isDbExist(Context context)
{
    final String DATABASE_NAME = "tippie";
    final String DB_PATH = context.getDatabasePath(DATABASE_NAME + ".db").getAbsolutePath();
    SQLiteDatabase db;
    try
    {
        db = SQLiteDatabase.openDatabase(DB_PATH, null,
                SQLiteDatabase.OPEN_READONLY);
        db.close();
    } catch (SQLiteException e) { return false; }
    return true;
}

which always return false.

Is there any way to check whether Room Database has been initialized?

homerun
  • 19,837
  • 15
  • 45
  • 70
  • Duplicate of https://stackoverflow.com/questions/49255502/how-to-check-if-a-db-exists-in-android?noredirect=1&lq=1 – Ashok Kumar Sep 11 '19 at 01:52
  • @AshokKumar Don't be quick to judge my friend.. The titles indeed the same but my approach to the problem is different. Also, if you minded to check on that given duplicate question you could at least see or test and beware that those solutions provided does not work except the one that uses a workaround to check if you have entries onto some entity which is not the legitimate way to perform this check if you ask me. Cheers. – homerun Sep 11 '19 at 07:30
  • You already know the dB file path , so you can check that the file exists it not . Also you could try the last answer given on the link. – Ashok Kumar Sep 11 '19 at 13:01
  • @AshokKumar, You should've read my last response, none of the suggested solutions work. You can check it yourself. I indeed getting the path for the database file but results come false checking for existence – homerun Sep 12 '19 at 10:01
  • Possible duplicate of [How to check if a DB exists in Android?](https://stackoverflow.com/questions/49255502/how-to-check-if-a-db-exists-in-android) – forpas Sep 14 '19 at 13:34
  • @forpas you could eliminate the word `possible` from your sentence by just checking on the content inside this title you provided... – homerun Sep 15 '19 at 08:05
  • @homerrrrrrrr this is no my comment. It is automated by the system when a question is flagged as a duplicate. – forpas Sep 15 '19 at 09:02

2 Answers2

1

Is there any way to check whether Room Database has been initialized?

If the room database has been built/opened (initialised) then it will contain a table called room_master_table.

Therefore, you could, if the open is successful, run a query to see if that table exists.

e.g.

Cursor csr = db.query("sqlite_master",null,"name = ?",new String[]{"room_master_table"},null,null,null);
if (csr.getCount() > 0) {
    //db is a room database
} else {
    // db is not a room database
}
csr.close();
  • Note the above is in-principle code, it has not been tested or run and may therefore contain some errors.

which always return false.

I suspect the issue is that your are appending .db to the database name. The .db extension is not necessary. If the database name is used without the extension when opening the database(building it in room) then the file name will not have the extension.

You would likely not have issues if you always just use DATABASE_NAME so :-

final String DB_PATH = context.getDatabasePath(DATABASE_NAME).getAbsolutePath();
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • It worked! i had to eliminate the .db extension as you said and it all working good now. Thank you kind sir. – homerun Sep 15 '19 at 08:37
0
private fun databaseFileExists(): Boolean {
        return try {
            File(getDatabasePath("MyInfoDatabase.db").absolutePath).exists()
        }catch (e: Exception){
            false
        }
    }
Manan
  • 394
  • 4
  • 15