Android SQLite Database Tutorial

Android have many method to store local data in android device. Android SQLite database is one of the best method to allow smart phone to stores data in a text file. It is a good and efficient approach to store the data in the smart phones. Besides that, SQLite database is a light weight database and supports all relational database features. By the use of SQLite Database in Android, the records and data can be easily to create, retrieve, delete and modify by just using the simple SQL statements. In this tutorial, I will give you some example on implement the SQLite database in Android device. I will give all the steps and explain how it works.

SQLite Database Demo

Creating an Empty Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “SQLiteExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. After that click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich) and Click Next button.
4. Choose “Empty Project” and Click Next button
5. Lastly, press finish button.

Prepare a Database table

Try to prepare a database table you will need to create a SQLite database later. I name my table name as DBExample and below are the structure and content of the table.

Add a New Class

Right click your package name > New > Java Class. You can type any java class name for example My class name is DBHelper.

Add a new java class - Android SQLite

Extends SQLiteOpenHelper and edit the class

The Following are the source code of DBHelper.java class, this class should extends SQLiteOpenHelper to enable the create, retrieve, update and delete features.

void onCreate(SQLiteDatabase db)
– This method will call when the database is create for the first time.

void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
– This method will activate when the Database version has been changed.

boolean insertContact (String name, String phone, String email)
– It allow to insert the contact name, phone and email to the contact table in the database.

Cursor getData(int id)
– Get the specific contact information from the contact table.

boolean updateContact (Integer id, String name, String phone, String email)
– It allow to update the contact name, phone and email to the contact table in the database.

Integer deleteContact (Integer id)
– It allow to delete specific contact record in the database.

ArrayList <String> getid()
– Get the all id from the contact table.

Edit activity_main.xml Layout

The activity_main.xml is to display all the contacts list of database and the add button. This xml is the layout of the MainActivity.java.

Edit MainActivity.java

This is the code in the MainActivity.java. This activity display all the contacts and allow to navigate to the DisplayContacts activity when any contact is clicked.

Create a new Activity to display contact

Add a new activity in your package (Right Click package name> New > Activity) and name it as “DisplayContact”. This activity is to display the contact information that selected by the user.

Edit the activity_display_contact.xml layout

Below are the activity_display_contact.xml to display the layout of DisplayContact.java. There have edit text to let user key in and the buttons to delete or edit the contact.

Edit DisplayContact.java code

Go to displaycontact.java class to paste the following code, the purpose of this class to perform the edit and delete feature from the DBHelper.java.

Edit strings.xml file

The following is strings.xml in the res/values package. This will show string value for xml and java file to make source code more efficient. By using this approach, the string can be reuse in every file.

Run Your Project

You are done to coding the SQLite database, now you can run the project in your android device to test out the features.

 (Android SQLite Database Tutorial)

Source Code

(Visited 633 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

3 Responses

  1. May 5, 2016

    […] to https://questdot.com/2016/05/android-sqlite/ and get your sqlite database in your […]

  2. June 9, 2016

    […] You can get sample SQLite project from this link SQLite Database Example. […]

  3. September 27, 2016

    […] provide SQLite database for local database in the android application. SQLite is hard to setting up because it […]

Leave a Reply

Your email address will not be published. Required fields are marked *