• 8 hours
  • Hard

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 4/27/23

Install the SQLite Room database

How do we store structured data?

In this section, we will focus on how to save structured data on our users’ phones.

It’s easy, we did it in the last section! Open a file, and store our structured data inside, right? 🙂

Well, not really!  😉 When I say structured data, I mean data that has strong relationships between them. Generally, this type of data is stored in a relational database. In Android, you have the ability to store this data in a database called SQLite.

SQLite is a relational database engine that can be fully manipulated with the SQL language. Unlike traditional database servers such as MySQL, PostgreSQL or Microsoft SQL Server, the entire database is stored in a file (not on a remote server). Because it takes up so little space (the source code is less than 2 MB), SQLite can be found in a lot of software such as web browsers, embedded systems, and operating systems like Android. 🙂

Historically, manipulating an SQLite database in Android was... how to put it... quite complicated! First, because SQL queries were hard-coded in static string variables:

private static final String SQL_CREATE_ENTRIES =

   "CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +

   FeedEntry._ID + " INTEGER PRIMARY KEY," +

   FeedEntry.COLUMN_NAME_TITLE + " TEXT," +

   FeedEntry.COLUMN_NAME_SUBTITLE + " TEXT)";

The disadvantage of this method is that there is no option for verification by your IDE (Android Studio) at the time of the compilation. This means that if there is an error, you'll only see it when your app crashes! 😱

Secondly, because you natively needed a lot of code to transform the result of a query into a Java object. Now, ORMs have begun to appear to compensate for these problems!

Uh, what’s an ORM?

Well, an object-relational mapping is a programming technique that allows developers to deliberately create the illusion of manipulating an object-oriented database, when in reality, you're working with a relational database. ORM will allow you to create an extra abstraction layer, so you can manipulate a relational database (like SQLite!) with objects. 🙂

There are many open source ORM projects for Android, such as greenDAO and OrmLite, with the goal of manipulating an SQLite database more easily. There is also the ORM realm.io, which is also very popular in Android. 😉 All this tinkering led Android to take things into its own hands, releasing its own ORM: Room!

In this part, we’ll be using Room to facilitate the storage of our structured data in Android’s SQLite database. To do so, we’ll install Room in our SaveMyTrip Android project, editing our build.gradle file as usual.

Excerpt of build.gradle:
dependencies {
   ...
   //ROOM
   implementation 'android.arch.persistence.room:runtime:1.1.1'
   annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
   //VIEW MODEL & LIVE DATA
   implementation "android.arch.lifecycle:extensions:1.1.1"
}

Sync your project, and that's it! In the next chapter, we’ll go into a little more detail about Room by creating our first entities.

Let's recap!

  • To store structured data, use a database!

  • SQLite is a relational database engine which can be manipulated using the SQL language.

  • An ORM, or object-relational mapping, allows developers to manipulate a relational database with objects.

  • Room is an ORM specifically developed by and for Android.

Example of certificate of achievement
Example of certificate of achievement