Is there a way to use SQLiteDatabase with Room?

Viewed 134

I need to be able to perform a some operations that would be easily done with SQLiteDatabase but I'm using Room. Is there a way for me to get an instance of a SQLiteDatabase object for that Room database? I've already tried the RoomDatabase object but the operations available are pretty limited compared to SQLiteDatabase.

2 Answers

RoomDatabase has getOpenHelper(), which returns a SupportSQLiteOpenHelper. As with SQLiteOpenHelper, SupportSQLiteOpenHelper offers getReadableDatabase() and getWritebleDatabase() methods. Those return a SupportSQLiteDatabase.

SupportSQLiteDatabase is not identical to SQLiteDatabase, but it is close, and it is supported by more SQLite implementations than just the framework SQLite.

Yes, you can easily get an SQLiteDatabase BUT NOT via Room.

e.g. assuming the Database Name passed to the databaseBuilder is defined as a constant DATABASE_NAME in the TheDatabase class then you can use. Also that a_context is a context (e.g. by using this in an activity)

 SQLiteDatabase sqlitedb = SQLiteDatabase.openDatabase(a_context.getDatabasePath(TheDatabase.DATABASE_NAME).getPath(), null, SQLiteDatabase.OPEN_READWRITE);

Obviously, you need to be careful using both Room and the above (probably ensuring that the each issues a close before the other opens).

Related