Contain details explanation about how to store data locally in android. Also, contain how to use Room which is android architecture component's material.
SQLite is described with basic query.
9. Store data locally
Anjan Debnath
SQLite in detail
● Define a schema and contract
● Create a database using an SQL helper
● Put information into a database
● Read information from a database
● Delete information from a database
● Update a database
● Persisting database connection
● Debug your database
10. Store data locally
Anjan Debnath
Define a schema and contract
● One of the main principles of SQL databases is the schema: a formal
declaration of how the database is organized.
● The schema is reflected in the SQL statements that you use to create your
database.
● You may find it helpful to create a companion class, known as a contract
class, which explicitly specifies the layout of your schema in a
systematic and self-documenting way.
12. Store data locally
Anjan Debnath
Create a database using an SQL helper
● Once you have defined how your database looks, you should implement
methods that create and maintain the database and tables.
13. Store data locally
Anjan Debnath
Put information into a database
● To access your database, instantiate your subclass of SQLiteOpenHelper
● Insert data into the database by passing a ContentValues object to
the insert() method
14. Store data locally
Anjan Debnath
Read information from a database
● To read from a database, use the query() method, passing it your selection
criteria and desired columns.
● The method combines elements of insert() and update(), except the column
list defines the data you want to fetch (the "projection"), rather than the data
to insert.
● The results of the query are returned to you in a Cursor object.
16. Store data locally
Anjan Debnath
Delete information from a database
● To delete rows from a table, you need to provide selection criteria that identify
the rows to the delete() method.
● The mechanism works the same as the selection arguments to
the query() method.
18. Store data locally
Anjan Debnath
Update a database
● When you need to modify a subset of your database values, use
the update() method.
19. Store data locally
Anjan Debnath
Persisting database connection
● You should leave your database connection open for as long as you possibly
need to access it. Typically, it is optimal to close the database in
the onDestroy() of the calling Activity.
20. Store data locally
Anjan Debnath
Room Persistency Library
● The Room persistence library provides an abstraction layer over SQLite to
allow for more robust database access while harnessing the full power of
SQLite.
● Why Room?
● You have to write long repetitive code, which will be time-consuming as
well as prone to mistakes.
● It is very difficult to manage SQL queries for a complex relational database.
22. Store data locally
Anjan Debnath
Room in detail
● Add the gradle dependencies in the build.gradle file.
● Create a data model class for the database table and annotate its table
name and primary key.
● Create an interface class for Database access. Create abstract methods for
CRUD operations. Add custom SQL query as a method.
● Create a Database class for database implementation.
● Declare and initialize an object for the Database class in your Activity or
Fragment class.
29. Store data locally
Anjan Debnath
Referrence
● https://developer.android.com/training/data-storage/sqlite
● http://demispb.blogspot.com/p/android-development.html
● https://developer.android.com/training/data-storage/room/
● https://medium.freecodecamp.org/room-sqlite-beginner-tutorial-2e725e47bfab
30. Store data locally
Anjan Debnath
Assignment 1 (Database)
● Create a Movie database.
● Create 2 table name movies and directors.
● The movies table must have movie name and movie id as primary key.
● The directors table must have director name and director id as primary
key.
● Create relation between 2 tables with foreign key.
● Using both SQLite and Room library you have to design your Database
schema and tables.
31. Store data locally
Anjan Debnath
Assignment 2 (Android App)
● Create an Android app that must have 2 below Activity.
● Activity 1 should have 2 Edit text and a submit button.
● It should be the Sign up page and you should enter user name and password.
● This user name and password should be saved into Shared Preference.
● Use the user name and pass to Sign in.
● Activity 2 should have 2 Edit text and a submit button.
● You have to input Movie name and Director name and clicking on submit
button it should be insert into Database.
● Use a Recycler View to display the Records from database.