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

Data type
Primary key

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/ 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.

Related Posts

  • 22 January

    FizzBuzz program in Python

    In this post, we will see how to program FizzBuzz in Python. As per wikipedia, Fizz buzz is a group word game for children to teach them about division. Here are the rules of the game: First player starts the game by saying number 1. Next player says next number but fun part is If […]

  • Format double to 2 decimal places in java
    22 January

    7 ways to format double to 2 decimal places in java

    Learn about how to format double to 2 decimal places in java

  • Escape double quotes in String in Java
    19 January

    How to escape double quotes in String in java

    In this post, we will see how to escape double quotes in String in java. There are scenarios where you need to escape double quotes already present in the String. This generally happens while dealing with JSON file format or reading file data. Escape double quotes in java Double quotes characters can be escaped with […]

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read also: Convert LocalDate to Date in java Using toInstant() method of Date class You […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using Instant object which we can from Zone. Here is […]

  • 04 January

    How to change java version in intellij

    Learn about how to change java version in intellij.


Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.