Saving Data with an ObjectBox Database on Android
- When to Use a Local Database
- Types of Databases
- Choosing the Right Type of Database
- CRUD Operations
- Getting Started
- Integrating a Relational Database
- Adding ObjectBox to Your Project
- Creating the Database Entry Structure
- Creating the BoxStore
- Add Entities to the Database
- Reading From the Database
- Editing an Entity
- Deleting an Entity
- Sorting the Entities
- Searching for Entities
- Integrating a Document-Oriented Database
- Where to Go From Here?
In this tutorial, you’ll learn about saving data with an ObjectBox database on Android. You’ll also learn about the different types of databases, the pros and cons of each type and how to implement them.
A local database is a file that allows your app to store a collection of data on a device. It’s useful when you want your app to persist data even if the user closes and reopens the app.
Local databases become especially important when you want to store a list of things. For example, you might want to store a list of the webpages the user has bookmarked on a browser.
To do this, you’ll create an app that lets you store a list of items that people have borrowed from you. You can change the borrowed item, the borrower and the date the person borrowed the item. The app also lets you search for items and filter based on the name of the item or date borrowed.
Before you get started, it’s time to review some background information.
When to Use a Local Database
While local databases can be very useful, they aren’t necessary for every situation.
You should use a local database when you want to persist app data across app sessions. They’re also helpful if you want to store a large volume of data that should be easy to access and manipulate. You can also use local databases to store a collection of data, such as a list of borrowed items.
Local databases aren’t meant for storing a few primitive values. You also wouldn’t use them to store user preferences.
Next, you’ll review two basic types of databases.
Types of Databases
You can classify databases by how they store data. By this method, there are two types of databases: relational databases and document-oriented databases.
Relational databases store data in tables. Each column of the table represents a data property. Each row in the table represents a data entry.
You define the column names and the type of data they can contain. Together, they are the schema. You do this using a language specified by the database.
SQLite is an example of a relational database.
In document-oriented databases, you store data as a collection of documents. Each document corresponds to one data entry. You can think of them as a series of files in a file cabinet.
Couchbase and Realm are both document-based databases.
Choosing the Right Type of Database
The kind of database you should use depends on what you need it to do.
If you want your database to be consistent, and the structure of your data is unlikely to change, you should consider a relational database. For example, a relational database would work well for an app that stores only user contact information.
If you want to store data that doesn’t have a fixed structure and is variable, you should consider a document-oriented database. It lets you store documents without mentioning the document structure beforehand. An app that stores responses received from a server that are likely to change often would benefit from using a document-based database.
Now that you’ve reviewed when to use databases and what types exist, you’ll take a look at the operations commonly used in them.
CRUD stands for Create Read Update Delete. These are the basic operations performed on every database.
Consider the app you’re going to make. It stores a list of borrowed items. Here’s the details of each operation:
- Create: This operation creates a new record in the database. If someone borrows a PlayStation game from you and you want to add it to the database, you’ll create a new record.
- Read: This operation reads an existing record in the database. If you want to find out who borrowed the game and when, you’ll perform a read operation to get the details.
- Update: This operation updates an existing record in the database. If you want to change the date when your game was borrowed, you’ll perform an update operation.
- Delete: This operation deletes an existing record from the database. If your friend returns the game and you no longer want to keep a log of that, you’ll perform a delete operation.
You’re now ready to start building the app!
Download the materials using the Download Materials button at the top or bottom of this tutorial. Inside the materials, you’ll find a starter project and a final project. Open Android Studio and select the Open an existing Android Studio project option.
Select the starter project from the download materials folder.
Once imported, build and run the app. You should get a screen like this:
Right now the menu items don’t do anything. You’ll modify them later in the tutorial.
For now, the
FloatingActionButton on this Activity doesn’t perform any action.
The starter project consists of the following that you should know about:
- A MainActivity that contains a
- A BorrowRecyclerViewAdapter with an
ItemDecoration, to make the list look nicer.
- A DateHelper class used to format the dates.
- A BorrowedItemModel class which represents each borrowed item’s details.
Integrating a Relational Database
SQLite is the default relational database available for Android. Every version of Android ships with a specific version of SQLite. Thus, you can expect your database to function consistently across devices.
While the SQLite APIs provided by Android are very powerful, it’s generally not recommended you use them directly. There are several reasons for this caution:
- The APIs require you to write your database queries using Structured Query Language, or SQL. If you don’t already know SQL, this becomes an additional thing to learn.
- You don’t have compile-time verification for the query. So, if you’ve written an invalid query, the app will build fine but raise an exception at run time. That’s not good.
- You need to write a lot of boilerplate code to set up the database.
- You need to write a lot of code to convert the results of your SQL queries to Kotlin objects.
To solve these issues, use an Object Relational Mapper, or ORM. An ORM is a library that lets you write queries on your database using the object-oriented approach of another language, such as Kotlin.
Room is a popular ORM for Android that is built on top of SQLite. To learn more about Room, you can refer to this tutorial.
This tutorial covers the setup and use of ObjectBox, a popular document-oriented database.