SQLite or Realm: What is the Top Choice for Android App?

Written by nataliia-kharchenko | Published 2018/05/21
Tech Story Tags: android

TLDRvia the TL;DR App

When a user launches a mobile application, he or she interacts with a client-side or front-end. Yet, this is just a tip of the iceberg. The mobile application is a complex structure that contains huge amounts of information and is created based on various Android frameworks. To make it fast and reliable, you need to figure out where to store the data. At this point, you should pay attention to SQLite and Realm — the most popular databases for Android applications. Further, we are going to study them in details and reveal their pros and cons.

What Android databases exist

When searching for a database, you need to clearly understand where you will store the data. If you are going to store it on the server, then you can select from a great number of different options. If you decide to store it on the mobile device, then you actually have just two databases to choose from — SQLite and Realm.

1. SQLite

SQLite is one of the first embedded databases. It was created by Richard Hipp in 2000. Its main idea is to move away from the client-server architecture and fit all application data on the device itself. SQLite is the very popular database that is well-liked by many Android developers all over the world.

It is written in C. The number of supported programming languages is more than 30. Probably, it will be difficult to find a device with no application that uses SQLite. This is the first but not the only solution that comes to mind when you decide to develop the out-of-server app. An equally well-known database is Realm. Let’s take a closer look at it.

2. Realm

Realm is the SQL-based product. Using it, you can declare object relationships in the same manner you do it in an object graf of your programming language. Realm is typically used for mobile app development.

It is extremely popular among young developers who just start their path in programming. What exactly makes them opt for this database bypassing SQLite? To clarify this, let’s consider the most common problems that novices face when deploying SQLite.

What problems arise when using SQLite?

SQLite is a perfect solution for small and simple projects. If your app falls under these criteria, you will be completely satisfied with this old-school database. If not, you are likely to face the following problems:

SQLite issues

Security

SQLite has a very specific structure and a set of features that influence the organization of security in your app. Here are a few points that you should pay attention to:

  1. The database is stored in a single file

You are free to put such file in any place in the directory hierarchy. On one side, it is very convenient. On the other, you risk losing all the data if some uncontrolled process wipes the file out. To prevent it, you should set a strong protection at the file level. Be careful when setting file permissions and avoid putting the file into the document root.

2. The journal files are not always deleted

You are provided with the opportunity to roll back as the system keeps the journal files during a certain period of time. This is certainly a good news. However, the bad one is that sometimes these files do not disappear. It may lead to the negative effects such as data corruption.

Another piece of bad news is that you can’t turn the journalling off as you may have your database damaged if the app crashes. To avoid these problems, ensure your data is strongly encrypted. Only after that, put it into the database. You can even use a particular app specially designed for this purpose.

Flexibility

The older your application becomes, the more data accumulates in the database. To maintain app productivity at a high level, you need to write more complicated queries and properly organize data migration. In most cases, novice programmers do not have enough experience in this area and face difficulties in using the structured query language.

Complexity

If you’ve been dealing with SQL and SQLite during a long period of time, you will have no difficulty in handling the situation described above. At the same time, a complex database structure may become a complete nightmare for newcomers in Android app development. For this reason, the vast majority of newbies lean toward an alternative option for data storage that does not rest upon SQL. This is when Realm comes to rescue taking its ease of use in vain.

What is different between SQLite and Realm?

Realm is an extremely popular database due to its numerous advantages. Cleveroad developers are familiar with it not by hearsay. They’ve been testing it out on multiple projects. Here are the main pros that we’ve revealed in practice:

Pros of Realm database for Android applications

  1. Overall simplicity

Realm is very easy-to-understand and simple-to-use. Compared to SQLite, it is very brief. You will have to write far fewer lines of code when using it.

Take a look at the following example. Here is how the code looks like when creating a table with the use of SQLite:

Here is how it works with Realm. You should only take over RealmObject.

Let’s look at another example. Here is how the code for results list query would look in SQLite:

Here is what you will get in Realm:

You will need to write quite a complex code when setting up data migration. Here is a good example of how it is going to look like in case you need to migrate data for the Android project using Realm database.

Also, you would need to add the following lines in config:

With SQLite, there are a few pitfalls that you are going to encounter during data migration. The biggest disadvantage is that you can not completely delete or rename a table or column. You can only copy data from the old table to the new one. After that, drop the old table and rename the new one. You should act in a similar way if you want to remove or add constraints from or to the table.

Speaking about the simplicity of Realm, we can not fail to mention that it is very intuitive and user-friendly. Definitely, you will puzzle it out much faster than SQLite. Since Realm is a fully-featured ORM platform, it won’t take long to read and save an object. Also, there is no need to worry about the internal collections. Everything is cut and dried.

2. High speed

Realm is a truly fast database. In some cases, the performance of the routines goes even faster than in SQLite even though the SQL algorithm is far from complex.

3. Clear documentation

Realm has a well-structured documentation written in a simple language. You can easily find the answers to any questions. The documentation is available online on the official website.

4. Fast accessibility

You can copy objects that are still available outside the Realm database.

5. Extra options

A young company Realm offers an advanced functionality with a variety of interesting options. Among other services, you can benefit from JSON and encryption support. Also, you can use a free API for your Android projects and receive notifications when the data changes.

All the above explains why a lot of developers prefer exactly Realm. Its simplicity and flexibility make it a real finding for newcomers and professionals. Yet, SQLite is not going to give up. At this point, we cannot fail to mention Room which was recently released.

What is Room and how does it work?

Room is a specially designed library. Google has created it in order to eliminate the annoying drawbacks of the SQLite. It acts as if abstraction layer that covers the database and changes the way the system works for the better. In some points, it even gives SQLite advantages over Realm. Here are the main problems that Room solves:

How Room improves SQLite

Encryption

SQLite did have some lacks in organizing proper encryption. With the help of Room, it provides keen and tough competition to Realm.

Live Data

Room facilitates the rapid and easy use of live data. No more confusing interruptions!

Size

Compared to Realm, Room is really small. Just think of 50KB vs 3 MB. The obvious advantage is in favor of Room here.

Data migration

With Room, you can perform data migration in a fast and easy manner.

Multithreading

Unlike Realm, Room is not going to cause any difficulties in the process of multithreaded mode. Everything goes smoothly.

Bottom line

As you can see, each database for Android app development has its pros and cons. While Realm is tasty-looking with its plainness, SQLite may scare with the complexity and some issues while in operation.

On the other hand, Realm cannot cover all the needs of the large-scale projects and is more suitable for small applications. SQLite teaming up with Room can be an excellent solution for complex tasks.

Despite the fact that SQLite has a number of pitfalls, you are unlikely to have a tough time with it. Google has worked hard to remove the plaguy drawbacks when creating Room.

Therefore, do not rush, analyze the scale of your project, and decide which database meets your needs.


Written by nataliia-kharchenko | Technical Writer
Published by HackerNoon on 2018/05/21