Local Databases in Android
In this blog, I have listed some Local Databases in Android that can be used for all DB purposes on the mobile client and also some detailed information about each of them :
- SQLite
- Realm
- ORMLite
- LevelDB
- Snappy DB
- CouchBase Lite
- Berkeley
- Room DB
Let’s get started!
SQLite :
SQLite is relational DB, a lighter version of SQL designed for mobile. It is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.
SQLite supports all the relational database features and is an open-source compact library.
It needs very little memory to operate AND Realm is much faster than SQLite. Realm can query up to 57 records/sec, whereas SQLite can do only up to 20 records/sec.
Realm :
Realm is a relational database management system that is like conventional databases, data can be queried and filtered, interconnected, and persisted but also have objects which are live and fully reactive.
Realm DB is developed by Realm and specially designed to run on mobile devices. Like SQLite, Realm is also serverless and cross-platform. It can be stored both on a disk as well as in memory.
The Realm is a mobile platform and a replacement for SQLite & Core Data. According to the website, it has more than 100k active developers. Realm Mobile Database is much faster than an ORM
Realm has so many advantages over native SQLite, like:
Objects in Realm are native Objects, You don’t have to copy objects out of the database, modify them, and save them back — you’re always working with the “live,” real object.
Objects always stay in sync.
The realm is much faster than SQLite. Realm can query up to 57 records/sec, whereas SQLite can do only up to 20 records/sec.
Data can be secured with transparent encryption and decryption.
Realm has a reactive architecture, which means it can be directly connected to UI if data changes it will automatically refresh and appear on the screen.
It automatically syncs to the Realm Object Server (if present) when there is network connectivity present.
One application can have multiple Realms, both local and remote
It can set different permissions for different users.
Available for Android, iOS, JavaScript, etc.
Reference:- Realm DB
ORMLite
ORMLite is a lighter version of Object Relational Mapping which provides some simple functionality for persisting Java objects to SQL databases.
ORMLite is helpful in big-size applications with complex queries because it handles “compiled” SQL statements for repetitive query tasks.
But ORMLite does not fulfil all the requirements, like it is bulky as compared to SQLite or Realm, slower than SQLite and Realm.
Reference:- ORM Lite DB
LevelDB
LevelDB is an open-source library that implements a key-value store, where keys and values are byte arrays, and data is stored ordered by key.
LevelDB supports atomic batch updates, forward and backward iteration over the contents of the store, snapshots (i.e. consistent read-only views of the entire store), caching, data integrity, and automatic data compression.
LevelDB is not an SQL database. Like other NoSQL and dbm stores, it does not have a relational data model and it does not support SQL queries. Also, it has no support for indexes. Applications use LevelDB as a library, as it does not provide a server or command-line interface.
Reference:- LevelDB
Snappy DB
Snappy DB is a key-value database for Android; it’s an alternative for SQLite if you want to use a NoSQL approach.
It allows you to store and get primitive types, but also a Serializable object or array in a type-safe way.
Snappy DB can outperform SQLite in reading/writing operations.
I came across a really lightweight key value-based database like shared preferences which was snappy DB.
It is similar to Shared Preference but not optimized enough. It’s an open-source DB so one can update it according to your needs.
Reference:- Snappy DB
I also found some statistics between SQLite and Snappy that can be used for comparison:-
Reference:- SnappyDB vs SQLite
CouchBase Lite
Couchbase Lite is a powerful NoSQL embedded JSON database. It is a highly scalable DB with enterprise-level security.
Data in Couchbase Lite is stored as JSON documents. Each document can have one or more attachments which are stored and loaded separately from documents.
Couchbase Lite runs locally on the device and persists data In JSON and binary format.
So if there is a requirement of any NoSQL DB in mobile OS, Couchbase lite is the best bet as it is very fast, reliable, and moderate in size.
Reference:- CouchBase Lite
Berkeley DB
Berkeley DB is an open-source high-performance embedded DB that allows us to handle data in different ways:
It can be in a relational way like SQLite (by replacing SQLite with its own library), or it can be in Key/Value pair data as byte arrays and supports multiple data items for a single key. It also supports java objects as data or it can also be XML documents.
The combination of Berkeley and SQLite is considered faster and performs better in concurrent and single writing multiple reading operations.
Reference:- Berkeley DB
Room DB
This library was introduced in Google IO, 2017 with the Android Architecture Component.
The room provides an abstraction layer over SQLite that allows us to use the full power of SQLite, it minimises the runtime crashes of the app.
Room is more efficient than SQLite DB.
Features
- Compile-time verification of query,
- Automatic updation of queries when the schema changes.
There are three main components in the Room:-
1. Database
2. Entity
3. DAO
Reference:- Room DB
Stay tuned for implementation guides of the above databases coming out soon!
Thanks for reading! And if you enjoyed it, gimme a clap!
Happy Coding :)