Android Studio SQLite Database Example




SQLiteDatabase is a class that allowed us to perform Create, Retrieve , Update, and Delete data (CRUD) operation. In this tutorial we will show you how to use SQLiteDatabase to perform CRUD operation in Android.

If you prefer to look for more tables example, this tutorial might suit you Android Studio SQLite Database Multiple Tables Example

Tool Used
1. Android Studio 0.40 2.1.2

To Do
In this tutorial we will going to create an app that allow to Create, Retrieve, Update, and Delete, student record.. Easy? ya, is easy if you know how πŸ™‚

Android Studio SQLite Database Example

Table Structure
This is the Student table structure that going to use to store student detail information, we make thing simple so we only create 3 fields as image below πŸ™‚
Android Studio SQLite Database Example

Id is primary key and also auto increment number

Screen Layout
We will create 2 screens, first screen will display all students in the database which will look like this
Android Studio SQLite Database Example
Second screen which is detail screen, when user click on the listview item it will bring user to new screen which will look like this
Android Studio SQLite Database Example

Code The Layout
1. Let’s start to create a project and name it SQLiteDB, and the rest of step should be as standard, if you need guide on this step , please refer this link. These are the basic setting i’ve set.

2. In src > main > res > layout > activity_main.xml. Add ListView, Button into activity_main.xml and it will look like this.

You need to change the ListView id to android:id=”@+id/list” if you choose to extends ListActivity in your class or else you will get error –> Your content must have a ListView whose id attribute is ‘android.R.id.list’

3. Add another activity and we named it StudentDetail.java. The go to src > main > res > layout > activity_student_detail.xml. Add Button, TextView into activity_student_detail.xml and it will look like this.

4. When user click on the list item we want to show student detail activity. So we need an unique id to retrieve student detail and this id must come from listview. To do this we have 2 options

  • The easiest way (if you like ;)) you could put id and name into listview item, and show it to the user (bad UI design -_-‘) and when the user clicks on the code split the selected item and pass to student detail activity to retrieve the record. (i.e. ID_studentname will appear on the listview item)
  • Create a new simple layout to help us to retrieve student detail by student id and hide the student id in listview item from user. (I like this, or you should do this way :)).

So to do with 2nd approach we need to create a new layout and name it view_student_entry.xml, and the code will look like below

Code
1. Now let’s start code on table structure portion first – create table in Android. Base on the table structure image above, we will create a file name Student.java , and the code in this file will look like this

2. In order for us to create a table for need to use these classes SQLiteDatabase, SQLiteDatabase is the class for us to perform CRUD function, and SQLiteOpenHelper, SQLiteOpenHelper is a helper class to manage database creation and version management. We’ll create a class and name it DBHelper.java and code in this file will look like this, to ease on understand code line by line i put comment in code. πŸ™‚

3. With above 2 steps, student table will be created when app get started and now we could code the CRUD functions! Create StudentRepo.java, the purpose of this class is to perform CRUD on the Student table. The code in this file will look like this.

4. When user click on the listview item we will display student detail information on detail screen, we will have this code to do the job for us. Go to src > main > java > StudentDetail.java. and copy this code into the class.

5. We have almost all pieces in place to display data for us in listview. So we continue in MainActivity.java, paste this code into file.

6. That’s all, Run! You should see the below result. πŸ™‚
Android Studio SQLite Database Example

Android Studio SQLite Database Example

If everything go smooth, database will be created in this path “data > data > SQLiteDB > databases > crud.db”, if you like to browse data and table structure of the database you could refer to How to browse Android Studio Emulator SQLite Database

What’s next?
So far we have shown you how to CRUD data locally without needed access internet, the next post we will show you how to pass data back to server and save in the SQL Server, How to Call ASP.Net Web API from Android Studio.

We got multiple tables example now, Android Studio SQLite Database Multiple Tables Example

Updated on 18 Mar 2015
This step is optional and only for those who are using eclipse
I noticed some reader had stuck in the StudentDetail.java file when they use Eclipse to try out this tutorial, the reason why you click “Add” and failed is because you need to import android-support-v4.jar and android-support-v7-appcompat.jar into your workspace, the detail you could follow this link

Alternatively, you use the this source code which build in Eclipse IDE 23.0.2.1259578 and the only different with the Android Studio version is below code

Updated On 02 Nov 2015
Remove the auto generated functions by IDE, the onCreateOptionsMenu and onOptionsItemSelected from the article, but still keeping these functions in the downloadable project.

Hope you enjoy the post.




JOIN OUR NEWSLETTER
I agree to have my personal information transfered to MailChimp ( more information )
In case you want to get any future post please allowed us deliver new posts direct into your inbox Or follow us on
We hate spam. Your email address will not be sold or shared with anyone else.