Android Tutorial – Step By Step Learning Android Sqlite Database for Beginner

Seegatesite – Today i will share an article about  Android Tutorial – Step By Step Learning Android Sqlite Database for Beginner . To learn android sqlite database, it is good we know the various data storage media which are owned by the android. Most applications require a medium for storing information inside. There are a lot of data storage options of android.

Here are the locations that can be used to store data permanent and most commonly used :

  • Shared Preferences : Permanent data storage in the form of key pair.
  • Internal Storage : locations where we can store files in the internal storage of the device. By default, files will be stored in the internal storage that is part of the application. When uninstalling the application, then the internal storage will also be deleted.
  • Local cache : if we want to cache data and open the store, we need a method getCacheDir () on the object activity or context.
  • External Storage : such as SD Cards and other external media.
  • Database Sqlite : A reliable database which I will discuss in this article.

Introduction Sqlite Database

Sqlite database is open source embedded database on android. To use sqlite in android application, available package by name android.database.sqlite. There are three main components that are used to handle sqlite in android

1.SQLiteOpenHelper

SQLiteOpenHelper used to determine the name and version of the database that will be used, in this class there is a onCreate() and onUpgrade() method. onCreate() invoked if the database doesn’t exist. Moreover this method is used to update the database schema the most recent.

SQLiteOpenHelper have getReadableDatabase() and getWritableDatabase() method  in order to access to the object SQLiteDatabase, either in read or write mode

2.SQLiteDatabase

SQLiteDatabase is the base class for sqlite database in android. This method is used to execute multiple SQL syntax such as query, update, insert and delete. SQLiteDatabase have many methods such as insert(), update(), delete() and also execSQL() used for the execution of the database directly.

In order the insert() and update() can work well, we need to define the object ContentValues key / value to the table. Key presented the identity column in the table. Value present the records in the table in the column.

A query can be made using the rawQuery() and query() method through SQLiteQueryBuilder class. Method rawQuery() accepts a SQL statement as input. While the method query() has a structured interface to specify the SQL query.  SQLiteQueryBuilder is a class that allows you to create SQL queries.

rawQuery()

Cursor cursor = getReadableDatabase();
rawQuery("select * from table where address = ?",new String[]{id});

Query()

return database.query(database_table,new String[]{key_1,key_3,key_2},null,null,null,null,null);

3.Cursor

Query function to return the cursor object, so that the cursor is the result of a query, one row (record) of the query results. With android cursor can perform buffer query results with efficient because it doesn’t need to load data into memory.

Several methods are commonly used

  • getCount() : To get the number of elements of the query results.
  • moveToFirst() : to move to the beginning of the line
  • moveToNext() : to move to the next line.
  • isAfterLast() : to check whether the last record of a query

Immediately, we learn through examples

Step By Step Learning Android Sqlite Database with Simple Example

1. Create a new android project with the name SqlDatabase. Read the introduction of android studio for beginners if you don’t understand how to create a new project on android studio.

2. Create a new java file named DatabaseHandler.java and copy the following script

Explanation :

Learning Sqlite Android How To Create Save Method For Beginner

Method save() is used to store the new data into the database. This method has a parameter in the form of an object from the Person class. ContentValues used to assign values to the table column. Furthermore, the process of storing data only call methods insert() owned by SQLiteOpenHelper.

The inserts() has the following parameters

public long insert (String table, String nullColumnHack, ContentValues values).

For details please visit developer.android.com

Learning Sqlite Android How To Create Update Method For Beginner

Method update() is used to update old data in the database. The update process has the following parameters

public int update (String table, ContentValues values, String whereClause, String[] whereArgs)

For details please visit developer.android.com

Learning Sqlite Android How To Create Delete Method For Beginner

Delete method used to remove data from the database. Sqlite delete process has the following parameters

public int delete (String table, String whereClause, String[] whereArgs)

For details please visit developer.android.com

Android Tutorial Learning Sqlite Android How To Show Table From Query Method Sqlite

The following method is tasked takes one row of data in the database with the parameters held in the form id. This method returns an object that has a class book that corresponds to the parameter id.

Android Tutorial Learning Sqlite Android How To Show Cursor From Arraylist Sqlite Android

Taking all the available data in the database need to iterate on Cursor and then turn them into objects of persons and put on a list.

3. Creating an entity Person.java. As usual when it will perform the operation associated with the data we modeled the data into a Java class along with its property that adapts to a column in the table that we have made above.

4. Class to manipulate the data in the database is finished we make. Then we will finish the entire code and create a class above the main class / MainActivity.java. In this example we will see if the results DatabaseHandler class that we make are correct or not?. We checked through the Debug Log on android studio. Please copy the following code on MainActivity.java

Explanation :

To see the results of the activity above, please refer to the debug log you

Android Tutorial Learning Sqlite Android Showing Log Debug On Android Studio

The results are as shown below

Android Tutorial Step By Step Learning Android Sqlite Database For Beginner

How do I display the query results table sqlite to listview?

Learning Sqlite Android How To Show Table Result To Listview Android

1. Create a new layout with the name list_item.xml and copy the following code

Table row above will be displayed as a list of items in the ListView

2. Add listview on your activity_main.xml

To add list_item.xml the ListView using the following code

app:listitem="@layout/list_item"

3. Add scripts on DatabaseHandler.java we have made as follows:

4. Create a new class with the name PersonAdapter.java to display the query results in a TextView in the list_item.xml.

5. To display the data in a ListView, we will change the code on MainActivity.xml that we created earlier. Please copy the following script

Thus article about Android Tutorial – Step By Step Learning Android Sqlite Database for Beginner, hope useful

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

8 Comments

 Add your comment
  1. This tutorial is good for ones who only want to use SQLite directly. I think it is better to use an ORM.

  2. Thank you so much for this Tutorial, It really helped a lot!!

  3. Thanks Id Like your post, but, I have some problems creating my database, can you help me??
    I need to create a database that saves information from a checkbox like a yes o no, o true and false, please help me!!!

  4. I can’t download source code

  5. this is so helpful..Please provide source code.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.