banner_sqlite

Android SQlite Database Tutorial


SQLite is an Open-Source embedded SQL database engine. This provides relational database management structure for storing user defined records in the form of tables. SQLite is light weight when it comes to resource usage and it doesn’t need to have any server setup like other RDBMS systems. It is an cross platform and self-contained database. SQLite is one way of storing user data. SQLite is a very light weight database which comes with Android OS .

Android is shipped with SQLite version 3.4.0.

In this tutorial I am taking an example of storing Employee record in SQLite database. I am using a table called Employee to store Employee record. This table contains three columns emp_id (INTEGER), emp_name (TEXT), emp_phone_number (TEXT).

tabel_structure

Step 1) Create Employee model class

write Employee class with all getter and setter methods to maintain single contact as an object.

Step 2) Create DatabaseHelper class

Create a class named DatabaseHelper and extends it with SQLiteOpenHelper .After extending your class from SQLiteOpenHelper you need to override two methods onCreate() and onUpgrade()
onCreate() – This is called when database is created and in this method we create the employee table
onUpgrade() – This is called when database is upgraded like modifying the table structure, adding constraints to database etc.





Step 3) Add All CRUD Operations (Create, Read, Update and Delete)

We need to write all operations that are required to read and write database .Add the following code snippets in DatabaseHelper class

-> Insterting New Employee Record

-> Reading All Employee Records

-> Update Employee Record

-> Delete Employee Record

Complete DatabaseHelper Class

Step 4) Using DatabaseHelper in MainActivity.class

DownloadCode
github

Enjoy Coding and Share Knowledge

Show Buttons
Hide Buttons