Android SQLite Database CRUD example

In this tutorial, we are going to learn about Android SQLite database CRUD example.
Android SQLite is open source relational database which can be used for performing crud operations.
You don’t have to do explicit installation for it. It is available by default in android.

Let’s start with simple example:

In this example, we will create a listview to show Country list and support all the CRUD operation with the help of SQLite database. When you will add a new Country to the list or delete any existing country, it will be reflected in the database.

Database Table structure:

Table Name: Country

Column
Data type
Primary key
Id
Integer
Yes
CountryName
Text
No
Population
Long
No

Source code:

Step 1 : Creating Project

Create an android application project named “SQLiteDatabaseCRUDExample”.

Step 2: Creating Layout

Change res ->layout -> activity_main.xml as below:

You will see below screen in design view.

Step 3: Create Country class
Create a country class which will correspond to Country table in SQLite database.

Step 4: Create SQLiteDatabaseHandler for defining database operations.

We will create a class called SQLiteDatabaseHandler which will extend SQLiteOpenHelper and override onCreate and OnUpdate method.
We will also add some CRUD methods.

  • addCountry
  • getCountry
  • getAllCountries
  • updateCountry
  • deleteCountry
  • delelteAllCountries
All above methods will interact with SQLite database and perform CRUD operations.

Step 5: Creating layout for Row

As We have declared ListView widget in activity_main.xml. Now we need to provide layout for individual row.
  • Go to res -> layout
  • right click on layout
  • Click on New -> File.
  • Create a file named “row_item.xml” and paste below code in row_item.xml.

Step 6:  Creating ArrayAdapter for ListView

Before creating MainActivity, we need to create CustomCountryList class for custom ListView row.

This class is used to populating data for ListVIew. getView method is get called for drawing each row.When you click on edit, you will see a popup to edit country Name and population. If you click on delete, country will be deleted from listview and SQLite database.

Step 7: Creating MainActivity

Change src/main/packageName/MainActivity.java as below:

When you click on “Add Country” button, you will get a popup.You put Country name and population and Country will be added to the list.

Step 8: Running the app

When you run the app, you will get below screen:

Click on “Add Country” button and you will get below screen. I have put Country Name as India and Population as 10000.

When you click on save, India will be saved to database. You can similarly add China, Bhutan, and Nepal. You will get below screen.

Let’s edit population of china to 20000 to 30000.

When you click on save, China’s population will be changed to 30000.
Let’s delete Bhutan from list, click on delete corresponding to Bhutan row. When you click on delete, you will get below screen.

Add Comment