Android SQLite Example Application Insert Update Delete Truncate Operations

Android SQLite provides a mean for an application developer to save data locally in device memory in a structured form, which makes it very easy to read, update or delete information saved in multiple formats.

Android SQLite is the best way to save data locally in the device itself. SQLite acts as a local database for applications saving data in tradition columns rows architecture. It is suited where data is structured and repeating, for example, user data information. Here we will discuss the implementation of SQLite form beginning using API’s of android.database.sqlite package.

Data limits allowed in SQLite is explained here

Here I am going to create an application sample which will explain some basic Database table operations like Insert, Delete, Update, Truncate Table, Get Rows Count and Get All Rows saved in Table.

So let’s Get Started…

Before we start Let’s Have a look some application view which we are going to develop.

Step 1) Open Android Studio and create a new project with Empty Activity Selection.

 

Step 2) Next we will create “DataBaseHelper” which will extend “SQLiteOpenHelper” which is a helper class to manage database creation and version management.

 

Step 3) Create “UsersDatabaseAdapter” this class will have UPSERT Operation methods that we will execute in the database.

 

Step 4) In “MainActivity” get an instance of “UsersDatabaseAdapter” to create DATABASE and TABLE.

Before OnCreate add

In OnCreate method add

We will add some buttons to open respective Activities. We will add those button Methods as well, so MainActivity will look like this

activity_main.xml will look like this after adding all buttons

Step 5) Now we will add Three Activities and Two ListViews

Activities will be:

  1. activity_insert_row.xml
  2. activity_update_rows.xml
  3. activity_delete_rows.xml

How to add a new activity: Right Click on main package > New > Activity > Empty Activity

Here I will add one by one first will show layout code then Activity Code.

activity_insert_row.xml

 

InsertRowActivity.java

In InsertRowActivity we will show a form with three EditText fields and a submit button to save values to table using UsersDatabaseAdapter‘s insertEntry method

 

activity_update_rows.xml

updateRowsActivity.java

updateRowsActivity will have ListView to show all rows and each row will have an Update button to save values in table row

 

activity_delete_rows.xml

 

deleteRowsActivity.java

Now we will add two ListView layouts

How to add ListLayouts: Right Click on layout folder in res > New > Layout resource file

listviewupdate_row.xml

this ListLayout is for update row activity.

 

listviewdelete_row.xml

this List layout is for update activity

 

Step 6) Now we will add CustomList Adapter files, these will list the user data in ListViews for Update and Delete.

CustomListAdapterUpdateRows.java

CustomListAdapterDeleteRows.java

 

Step 7) Create a User Model to get and set data values.

UserModel.java

 

Phewww…  so we have added all the files we needed for this example app to work as expected. In most places, I have already added inline comments. let me know in comments if you have Questions…

Oops here is source code for above application 😛

Leave a Reply

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