The Support Database API
So far, this book has portrayed Room as being an ORM-style bridge between your code and SQLite.
Technically, that is not accurate.
The Room artifacts, such as
androidx.room:room-runtime, have transitive dependencies on
androidx.sqlite:sqlite-framework. Room itself talks to an abstraction around SQLite provided by
androidx.sqlite:sqlite. This book will refer to this as “the support database API”.
androidx.sqlite:sqlite-framework provides the default implementation of that abstraction, one that works with the built-in copy of
SQLiteDatabase (part of the “framework”). When we use
RoomDatabase.Builder to set up our
RoomDatabase, we are using those “framework” classes for the database access.
In this chapter, we will explore in greater detail why this support database API exists and how we can use it, because while most of the time we will be able to use Room-generated code to work with the database, sometimes we cannot.
“Can’t You See That This is a Facade?”
To many developers, SQLite “is what it is”. Android ships with a SQLite implementation, and we use it, either directly or via some form of wrapper library.
However, in truth, there are many SQLite implementations. After all, SQLite is a library, and so there is nothing stopping people from using a separate, independent copy of SQLite from what is in Android. Even in Android itself, what SQLite you get depends on what device you run on:
- Different API levels integrate different versions of SQLite
- Device manufacturers sometimes replace the stock SQLite version with another
And so, sometimes, we need a facade: an API that we can code to that supports a pluggable implementation. The following sections outline some examples.
Requery is a Room-like object mapping library, one that works both on Android and on the regular Java JVM. For plain Java (or Kotlin), Requery uses JDBC. For Android, Requery integrates with the support database API. Beyond that, Requery offers its own implementation of that API, wrapped around a standalone copy of SQLite. This ensures that you are using a current version of SQLite, even on older devices.
SQLCipher for Android
One of the best-known alternative SQLite implementations for Android is Zetitec’s SQLCipher for Android, which offers transparent encryption of database contents. As of version 4.3.0, SQLCipher for Android offers an implementation of the support database API. This allows you to use encrypted databases from Room. We will explore this in greater detail later in the book.
Just as Requery is an ORM that uses the support database API, so is SQLDelight. As with Requery, SQLDelight works on the JVM and Android. However, SQLDelight is a Kotlin/Multiplatform library, and it also supports Kotlin/Native for iOS and Windows. So, SQLDelight lets you write your database access code once and use it in multiple environments, and SQLDelight for Android lets you use the support database API so you can use the framework database, Requery’s standalone SQLite, or SQLCipher for Android as your actual database implementation.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.