Cupboard is a third-party abstraction layer over SQLite. In this video, learn more about the benefits of using a library like Cupboard to ease your SQLite database interactions.
- [Narrator] Another way to persist data in Android, is to use Cupboard. Let's talk briefly about what Cupboard is, and how you can use it to store your application's data. First, Cupboard is simply an abstraction layer over SQLite. It's main goal is to eliminate the amount of boilerplate code that you write when working with your database. Further, Cupboard makes it easy to interact with Android APIs, like ContentProviders, cursors, and ContentValues.
Having your model classes is very straightforward. The only potential change you need is to declare a field of type Long, with the name of _id. This is because by default, Cupboard creates the _id column for you that Android uses when working with cursors. To have a class added to the Cupboard data store, you simply need to register it. This is typically done in your database SQLiteOpenHelper class.
In this example, once we set up Cupboard, we end up with the following two columns in our recipe table. The _id field would be an autoincrementing one of type Long. And the name field would be of type Text. There's no need for us to define a separate contract class and table creation query. Additionally, Cupboard helps to eliminate the ceremony that goes into adding, updating and querying records in your database.
Recall that when we need to add a new entry to our recipe table, we have to first create a ContentValues object and then populate it with each field for the recipe that we want inserted. Conversely, by using Cupboard, this work is all reduced to this one line of code. Behind the scenes, Cupboard handles the conversion to ContentValues for you. What's more, Cupboard works seamlessly with the existing Android APIs.
For instance, if you were to receive a cursor back from a loader or a ContentResolver, you can still work with the data using Cupboard. It ships with a method called withCursor. This takes a cursor and allows you to interact with it through the abstraction layer that Cupboard provides. Thus, you can begin to work with your objects without the hassle of doing any intermediate mapping. This leads us to the question, when should you use Cupboard? I find it's best when you have simple entities and relationships among those entities, when you need to be able to work seamlessly with the existing Android APIs.
We talked about ContentProviders, CursorLoaders, and more. And finally, when you don't require a full ORM solution. Why is that? Cupboard does not deal with the relational mapping of objects. For example, if you have a list in your model class, similar to what we see here, by default Cupboard would throw an exception as it doesn't understand this type of field. In the cases where you don't need the relational aspect, but you still want to store a list of things with your entity, you can use what's known as a field converter.
And then the list will be embedded inside your Cupboard entity. It's typically stored as a JSON string. We're going to talk more about this in a future video. On the whole, Cupboard is a great way to abstract away the boilerplate often associated with dealing with SQLite on Android. From here, if we take a look at the website for Cupboard, you can see that it's installed as a Gradle compile time dependency.
It's simply added to your application's build.gradle file. Once you refresh your dependencies, then you will have access to the needed Cupboard classes.
Looking for study partners?Join the Associate Android Developer Exam study group
By persisting your data, you can provide users with a more seamless experience with your app—even when they go offline. If you're an Android app developer, you have many data persistence options to choose from. In this course, learn about various options for storing your app's data in a database.
To begin, Annyce Davis reviews using the de facto data persistence solution available on Android: SQLite. She takes you through basic SQLite concepts—such as how to create a database wrapper and insert data into a table—as well as a few more advanced topics. Next, she covers working with Cupboard, an open-source project dedicated to simplifying your SQLite interactions. To wrap up, she dives into working with Realm, an object database solution designed with mobile devices in mind. Throughout the course, Annyce discusses establishing relationships between database tables, creating and running queries, as well as performing data migrations.
- Overview of SQLite
- Defining a database table's schema
- Exploring SQLite create table syntax
- Executing create table statements
- Inserting data with a foreign key relation
- Inspecting the database using a terminal
- Updating and deleting records
- Working with Cupboard
- Working with Realm
Skill Level Intermediate
Q: This course was updated on 05/24/2018. What changed?
A: New videos were added that cover working with Room.