# Are there any existing alternatives for SQLDatabaseHook for Android Room

**URL:** <https://discuss.zetetic.net/t/are-there-any-existing-alternatives-for-sqldatabasehook-for-android-room/5115>\
**Category:** SQLCipher\
**Created:** [September 22, 2021, 3:58pm UTC](https://discuss.zetetic.net/t/are-there-any-existing-alternatives-for-sqldatabasehook-for-android-room/5115 "2021-09-22T15:58:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marzipan](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/marzipan/32/1676_2.png) [@marzipan](https://discuss.zetetic.net/u/marzipan)\
**Post date:** [September 22, 2021, 3:58pm UTC](https://discuss.zetetic.net/t/are-there-any-existing-alternatives-for-sqldatabasehook-for-android-room/5115/1 "2021-09-22T15:58:00Z")

</div>

From what I have understood of the README and Docs, I should use `SQLDatabaseHook` interface to execute my SQL statements in `prekey()` method then maybe clean up on `postKey()`. However, the only parameter for this interface’s method is a `SQLiteDatabase` object, which I am able to query on the `prekey()` hook using sql `query()`, so I guess it’s the sqlite equivalent of the room database object I build. I would however like to continue using my` RoomDatabase` objects as before rather than having to use queries but cannot find an equivalent hook I can extend to use a prepared `Roomdatabase` rather than `SQLiteDatabase` object as an argument, so that I can in turn use the `DAO`. Is this doable in SQLCipher?, all the implementations I’ve seen access the unencrypted DB as a `SQLDatabase`.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [September 22, 2021, 4:38pm UTC](https://discuss.zetetic.net/t/are-there-any-existing-alternatives-for-sqldatabasehook-for-android-room/5115/2 "2021-09-22T16:38:48Z")

</div>

Hi @marzipan,

The `SQLiteDatabaseHook` predates the Room/Support API. The purpose is to provide a seam for interfacing with the database connection immediately before and after the keying operation is performed. At the point of invocation, SQLCipher is not aware of a RoomDatabase instance per se, just the underlying connection which is exposed to the callbacks with a `SQLiteDatabase` instance.

---

<div class="post-metadata">

**Author:** ![marzipan](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/marzipan/32/1676_2.png) [@marzipan](https://discuss.zetetic.net/u/marzipan)\
**Post date:** [September 22, 2021, 4:50pm UTC](https://discuss.zetetic.net/t/are-there-any-existing-alternatives-for-sqldatabasehook-for-android-room/5115/3 "2021-09-22T16:50:22Z")

</div>

I had misunderstood it then, infact my warped understanding made me wonder if id have to dump the singleton pattern and build everytime i wanted to use database. What I needed to know then is how I would access the encrypted database consequently. Say I have encrypted it with this code `public abstract class OfflineDatabase extends RoomDatabase {  
public abstract RegistrationInfoDAO registrationInfoDAO();  
public static final String DB\_NAME=“SOME\_DB”;  
private static volatile OfflineDatabase DB\_INSTANCE=null;

```
public static synchronized OfflineDatabase getInstance(Context context){
    if (DB_INSTANCE!=null){
        return DB_INSTANCE;
    }
    else{

        String passwd="123";

        final byte[] passphrase = SQLiteDatabase.getBytes(passwd.toCharArray());
        final SupportFactory factory = new SupportFactory(passphrase);
        final OfflineDatabase offlineDatabase = Room.databaseBuilder(context, OfflineDatabase.class, DB_NAME)
                .openHelperFactory(factory)
                .build();
        return offlineDatabase;
}

```

}}`

My current attempt to access it using getInstance fails for any DAO operations ` OfflineDatabase offlineDatabase=OfflineDatabase.getInstance(appContext);` with a sqlcipher.\*.SQLException since database is already encrypted. The only alternatives I see online are using a SQLiteDatabaseObject `openOrCreateDatabase()` which can accept a password but that would not allow room operations. Any way to access my singleton instance while supplying a password? Thanks.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [September 22, 2021, 8:16pm UTC](https://discuss.zetetic.net/t/are-there-any-existing-alternatives-for-sqldatabasehook-for-android-room/5115/4 "2021-09-22T20:16:41Z")

</div>

Hi @marzipan

You certainly don’t want to recreate a database instance every time, that is cost prohibitive with SQLCipher due to the time required for key derivation. From your `getInstance(...)` function, it appears that you will want to assign `offlineDatabase` to `DB_INSTANCE` in your else branch. That should allow you to create a single instance of the Room database reference.

---

<div class="post-metadata">

**Author:** ![marzipan](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/marzipan/32/1676_2.png) [@marzipan](https://discuss.zetetic.net/u/marzipan)\
**Post date:** [September 23, 2021, 6:02am UTC](https://discuss.zetetic.net/t/are-there-any-existing-alternatives-for-sqldatabasehook-for-android-room/5115/5 "2021-09-23T06:02:48Z")

</div>

Hi @developernotes. Yes I will use the singleton pattern for subsequent requests. My issue is with how i can supply a passkey to unlock the database while obtaining an instance of it, attempts to obtain my room singleton instance will throw the SQLiteError as its (rightfully) encrypted.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [September 24, 2021, 5:20pm UTC](https://discuss.zetetic.net/t/are-there-any-existing-alternatives-for-sqldatabasehook-for-android-room/5115/6 "2021-09-24T17:20:57Z")

</div>

Hi @marzipan

> My issue is with how i can supply a passkey to unlock the database while obtaining an instance of it

You could provide that somewhere within the class that is exposing the singleton instance of the database.
