Developing Android Applications often involves storing, reading and updating data. From this point, the main question is which approach to choose? There are several ways which Android developers use to save data depending on their needs.
Here, we will present some of the most frequently used Database and their features. We can divide database approaches into categories like plain SQLite, ORMs and NoSQL database.
#1 SQLite Database
SQLite database is native to Android and it is used as a Local Database. It is open source database that supports standard features like SQL syntax, transactions and prepared statements.
- In general, primary advantage is its simplicity
- Simple to Administer
- Simple to Operate
- Simple to Embed in a Large Program
- Simple to maintain and customize
SQLite is used in most of the applications which need a less complicated relation database. It is written in C programming language and it is possible to translate it into any standard C compiler. Unlike most other SQL databases it is serverless, there is no intermediary server process, it is a compact library that reads and writes directly to ordinary disk files.
More about SQLite read here.
#2 ORMs (Object Relational Mapping)
ORM framework is written in an object-oriented language (Java, C#, PHP, etc…) and it is designed to virtually wrap around a relational database. It makes developers work easier. ORM often reduce the amount of code that needs to be written, they do most of the boilerplate code for the developer in the background but require learning a new APIs.
Object Relational Mapping Lite (ORM Lite) is a simple and lightweight JAVA ORM.
OrmLite is not an Android ORM, it is a JAVA ORM with SQL database support. It can be used anywhere Java is used, such as JDBC connections, Spring and also Android. OrmLite gives you the most features you need in a simple way and still gives you access to the SQL if needed.
Using OrmLite is pretty simple. For each table, create an object to represent the data and add DatabaseTable to the class. For each field, add DatabaseField. There are some details in the configuration of both which allow you to do most of the basics in the ORM space.
With ORMLite you can set up your classes by simply adding Java annotations. It has powerful abstract Database Access Object (DAO) classes. Also provides simple and flexible query using QueryBuilder, auto generates SQL to create and drop database tables and it has basic supports for database transactions. ORMLite Supports MySQL, Postgres, Microsoft SQL Server, H2, Derby, HSQLDB, and SQLite and can be extended to additional databases relatively easily.
OrmLite is open source and you can find on GitHub.
ActiveAndroid is an active record style ORM which allows you to save and retrieve SQLite database records without ever writing a single SQL statement.
ActiveAndroid is a simple-to-use Object Relational Mapper library, which maps Java classes to database tables and java class member variables to the table columns. Each row in a table will become an object. This will help the developer create, update, delete and select data from SQLite database using model objects without using raw SQL queries. Each database record is wrapped neatly into a class with methods like save() and delete().
AndroidActive you can found on GitHub.
Wherever I search and ask about GreenDAO I found one answer: GreenDAO = “fast”.
GreenDAO is considered to be the fastest ORM for Android.
Here are some of the reasons why you check out GreenDAO:
- Maximum performance (probably the fastest ORM for Android); our benchmarks are open sourced too
- Easy to use powerful APIs covering relations and joins
- Minimal memory consumption
- Small library size (<100KB) to keep your build times low and to avoid the 65k method limit
- Database encryption: greenDAO supports SQLCipher to keep your user’s data safe
- Strong community: More than 5.000 GitHub stars show there is a strong and active community
SugarORM is an ORM developed only for Android in order to simplify the interaction with SQLite database.
Sugar ORM was built in contrast to other ORM’s to have:
- A simple, concise, and clean integration process with minimal configuration.
- Automatic table and column naming through reflection.
- Support for migrations between different schema versions.
DBFlow is designed to be fast, high performance, and easy to approach. It reduces codes for dealing with databases with its powerful and simple API.
DBFlow on GitHub.
It is a modern SQL-based query & persistence library for Android. Requery provides a modern set of interfaces to perform and persist queries. It is object mapping and SQL generator with RxJava and Java 8 support.
Ollie is a compile-time active record ORM for Android. It supports RxJava.
Similar to other libraries, RushOrm replaces the need for SQL by mapping java classes to SQL tables. It can be used in projects with complex data structures and projects that communicated with a REST API.
Simple SQLite database layer for Android with minimum setup needed. It is Built-in RxJava support, Built-in AutoValue immutable objects support, Built-in Kotlin support, support for SQLite views, complex columns, migrations.
If you want to find an alternative to SQLite and its abstractions there are a few database approaches that use a data structure that is non-relational. NoSQL databases include key-value stores, document databases, wide-column stores, object databases, and graph databases.
Most NoSQL databases favour speed and store unstructured data. So, for mobile app handling large amounts of (unstructured) data or need a lot of flexibility, a NoSQL approach may be the solution.
Realm is a database platform with a distinct architecture, which has emerged as a new alternative to SQLite.
The key difference between Realm and ORMs is that Realm is not an abstraction built on top of SQLite, but a whole new database engine. Rather than a relational model, it is based on an object store. Its core consists of a self-contained C++ library. It currently supports Android, iOS(Objective-C and Swift), Xamarin, and React Native.
Some of the reasons why you should consider Realm:
- Easy modelling
- Retrieve Only Data You Need! Zero-copy Design
- It’s Not Just a Wrapper
- Community & Support
For more about Realm read here.
It is a NoSQL cloud-hosted database, data is stored in native JSON. Firebase enables quick and easy development saving time for writing server-side code or to deploy a complex server framework. You don’t have to think about how data would be stored, and synchronized across various examples of the application in real time. Data is synced across all clients in real-time and remains available when your app goes offline.
Some benefits of using Firebase are:
- provides a real-time database and backend as a service
- In situations where data is constantly changing due to the large number of users (database uses data synchronization—every time data changes, any connected device receives that update within milliseconds)
- Stored in the cloud so readily available everywhere
- Cross-Platform API (If you are using this DB with an App)
- Developers using the real-time database can secure their data by using the company’s server-side-enforced security rules
SnappyDB is a key-value database for Android. It allows you to store and get primitive types, but also a Serializable object or array in a type-safe way. SnappyDB can outperform SQLite in reading/write operations.
Couchbase Lite is an embedded JSON database that can work standalone, in a P2P network, or as a remote endpoint for Sync Gateway.
- Represents data with a flexible schema form, JSON
- Provides Native APIs for iOS, Android and .NET
- Supports replication with compatible database servers
- Supports peer-to-peer replication
- Supports low-latency and offline access to data, you work primarily with local data.
One of the very important steps in developing mobile applications is choosing the right database.
What to choose?
The best choice is to analyze your own needs, the app size and the aim you want to achieve. SQLite is native to Android, well known to developers and simple to use if you want to access it easier without a lot of boilerplate code you will probably choose one of ORMs. If you are dealing with a large amount of unstructured data NoSQL approach can be interesting for you.