1

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.

casolorz
  • 8,486
  • 19
  • 93
  • 200

2 Answers2

3

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

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).

MikeT
  • 51,415
  • 16
  • 49
  • 68