This document discusses different options for storing data on Android devices, focusing on SQLite databases and the Room Persistence Library. It notes that SQLite is a lightweight relational database included with Android that can store structured data, but implementing it directly requires boilerplate code. The Room Persistence Library provides an abstraction layer over SQLite that makes it easier to work with databases on Android by handling common tasks like converting queries to Java objects. The key components of Room are the Database class, Entity model classes, and Data Access Objects (DAOs) for accessing the database. The document provides steps for setting up a Room database, including adding dependencies, defining Entity classes, creating DAOs, and building the database.
2. Data Storage
on Android
• Androidprovides several optionsfor you to save
your app data.
• The solutionyou choose dependson your specific
needs, such as how much space your datarequires,
what kind of data you need to store.
• Different data storage optionsavailableon
Android:
Internal file storage:Store app-privatefiles on the
device file system.
External file storage: Store files on the shared external
file system. Thisis usuallyfor shared user files,
such as photos.
Shared preferences: Store privateprimitive data in key-
value pairs.
Databases : Store structured datain a private
database.
• Except for some types of files on external storage,
all these optionsare intended for app-privatedata,
the data is not naturallyaccessible to other apps.
4. SQLite Database for Data Storage and Retrieval
SQLite is very lightweight, open source, relational database that comes with Android OS.
In Android, integrating SQLite is a tedious task as it needs writing lot of boilerplate code
to store simple data.
Consider SQLite when your app needs to store simple and structured data objects.
Alternatively, Room Persistence Library could be used for better APIs and easier
integration.
5. SQLite
Challenges
Boilerplate code to convert
SQL queriestoJavaData
objects
Database operationsonthe
main thread
Unmaintainable code - As your
schemachangesyouneedto
update the affectedSQL
queriesmanually.
SQL queriescheckedat
runtime (Nocompile time
verificationof SQLqueries)
Untestable code
7. Room Persistence Library (RPL)
• Room is an abstraction on top of SQLite database. It's part of the
Architecture Components by Google.
• Room enables you to easily work SQLite databases in Android.
• The Room persistence library acts as a layer that abstracts raw SQLite
and lets you make use of robust database access while keep you close to
SQLite so it doesn't get in the way when you want to access low level
features of SQLite.
9. Components of Room
Library
There are 3 major components in Room:
• Database : Contains the database holder and
serves as the main access point for the underlying
connection to your app's persisted, relational data.
• Entity: Represents a table within the database.
• DAO: Contains the methods used for accessing the
database.
13. Add Dependecies
• Open the application level build.gradle file and add
the dependencies for Room:
Implementation "android.arch.persistence.room:runt
ime:1.1.0"
AnnotationProcessor "android.arch.persistence.room
:compiler:1.1.0"
testImplementation "android.arch.persistence.room:
testing:1.1.0"
15. Important
annotations
• @Entity – by using this annotation, Room
will know that this class will be a table in the
database and the table name will define the
name of the table
• @PrimaryKey – is used to set up the primary
key of the table.
• @ColumnInfo – defines the name of the
column from the table if we don’t want to
use the name of that field
• You also need to add getters and setters.
17. Details about DAO
• @Dao – by using this annotation Room knows that it should provide
the implementation for our interface by generating code for the
defined methods.
• In this interface we could define the CRUD operations for our entity and
also any other operations that are necessary in order to obtain the data.
• Also Room offers us query validation at compilation time, so if we will
write incorrectly the name of a table or a field we will know if after the
code will be compiled.