Intro
Scott Carson | 09/22/2016
All code for this series is located here.
I am going to come clean – I never took a databases course in college, I never had any formal SQL training, and as a result learning SQLite as a newbie Android developer was a challenge. Sure, basic SQLite statements are very human readable; it’s the database management aspect that really terrified me.
Yes – I said terrified.
Through a recent project I have become very closely acquainted with object-relational mapping in Android, specifically using the ActiveAndroid library. ActiveAndroid has made database management a breeze (as long as you stay on top of schema updates) and I would like to share some of what I have learned in this tutorial series.
In the next few posts I’ll be covering the following topics:
- What is Object-Relational Mapping (ORM)
- Setting up ActiveAndroid
- Defining a Model
- Saving to the database
- Querying the database
- Using a ContentProvider
- Custom TypeSerializers
- Schema migrations – Basic and advanced
To give credit where credit is due, the ActiveAndroid website is a great place to jump in and get started with the library. Much of what I will cover is covered in the ActiveAndroid documentation; however, I will be elaborating on some topics and giving more complex examples than are presented there. This tutorial assumes intermediate to advanced Java knowledge, but newbies to the Android platform are welcome.
What is Object-Relational Mapping (ORM)?
At a high-level the concept of Object-Relational Mapping is very straightforward. As Java programmers we have been trained to think in Object-Oriented (OO) terms. A basic definition of ORM is that it is “a technique to convert between incompatible type-systems in an object-oriented programming language”. In our case, we are trying to convert between the Object types we use to model our application and the basic types allowed in SQLite (INTEGER, TEXT, REAL, NONE).
In other words we have a square-peg and a round-hole.
The ORM translates the Java Object into a SQL table
That is where ActiveAndroid comes in. ActiveAndroid translates your high-level Java Object domain model into a SQLite database using simple annotations. It enables complex object graphs to be easily saved and queried, and you do not have to write a single SQLite statement. Some SQLite knowledge can come in handy when tailoring queries and performing schema migrations, but that will all be covered in this series.
One final disclaimer: by no means is ActiveAndroid the only ORM/database tool available for Android, and in some cases it may not even be the best to use. It is simply a tool that is fairly well documented, easy to learn, and easy to use. Other tools include Realm and OrmLite. Realm is an interesting library because it is written in C++ and built specifically for mobile applications. This means it avoids some of the overhead that SQLite brings into the picture. OrmLite is a Java (not Android specific) ORM tool that utilizes annotations much in the same way ActiveAndroid does.
What is next?
The the next post will look at setting up ActiveAndroid in a new project and defining your first Models.
Part 2: Setting up ActiveAndroid