Android SQLite Insert Data Into Database Tutorial

SQLite database is a self sustainable, self contained and self server config database with no additional server configuration requirement because it always runs on local server. SQLite database is mostly same as the SQL database but it is lighter than the SQL thus it can be easily use and configure by mobile developers. In this tutorial we will going to learn about some basic fundamentals of SQLite database and execute query on a already created DB. We would also insert data into SQLite database using EditText and store entered values into database tables.

Contents in this project Android SQLite Store Data Into DB from EditText.

  1. Watch the application demo video.
  2. Start a new application project in Studio.
  3. Adding two EditText and one Button in activity_main.xml file.
  4. Declaring SQLiteDatabase, EditText, Button objects and String, Boolean variables.
  5. Assign their ID’S to them.
  6. setOnClickListener on button.
  7. Write SQLite Database create function.
  8. Write SQLite Database Table create function.
  9. Check EditText is Empty or Not.
  10. Insert Data into SQLite database using query implement.
  11. Empty the EditText after done insertion process.
  12. Call all the 5 function inside the button click scope.

1. Watch the application demo video.

2. Start a new application project in Studio.

Start a fresh project in android studio or open your existing project in which you want to add SQLite functionality.

3. Adding two EditText and one Button in activity_main.xml file.

There are two EditText and first one is for getting name from user and second for get phone number. Button is used to submit data in database.

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:hint="Enter Name"
    android:ems="10"
    android:layout_marginTop="27dp"
    android:id="@+id/editText"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:gravity="center"/>

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:hint="Enter Phone Number"
    android:gravity="center"
    android:ems="10"
    android:layout_below="@+id/editText"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp"
    android:id="@+id/editText2" />

<Button
    android:text="Insert Into SQLite Database"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp"
    android:id="@+id/button" />

 4. Declaring SQLiteDatabase, EditText, Button objects and String, Boolean variables.

SQLiteDatabase sqLiteDatabaseObj;
EditText editTextName, editTextPhoneNumber;
String NameHolder, NumberHolder, SQLiteDataBaseQueryHolder;
Button EnterData;
Boolean EditTextEmptyHold;

 5. Assign their ID’S to them.

EnterData = (Button)findViewById(R.id.button);
editTextName = (EditText)findViewById(R.id.editText);
editTextPhoneNumber = (EditText)findViewById(R.id.editText2);

 6. setOnClickListener on button.

EnterData.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

    }
});

 7. Write SQLite Database create function.

public void SQLiteDataBaseBuild(){

    sqLiteDatabaseObj = openOrCreateDatabase("AndroidJSonDataBase", Context.MODE_PRIVATE, null);

}

 8. Write SQLite Database Table create function.

public void SQLiteTableBuild(){

    sqLiteDatabaseObj.execSQL("CREATE TABLE IF NOT EXISTS AndroidJSonTable(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR, phone_number VARCHAR);");

}

 9. Check EditText is Empty or Not.

public void CheckEditTextStatus(){

    NameHolder = editTextName.getText().toString() ;
    NumberHolder = editTextPhoneNumber.getText().toString();

    if(TextUtils.isEmpty(NameHolder) || TextUtils.isEmpty(NumberHolder)){

        EditTextEmptyHold = false ;

    }
    else {

        EditTextEmptyHold = true ;
    }
}

 10.Insert Data into SQLite database using query implement.

public void InsertDataIntoSQLiteDatabase(){

    if(EditTextEmptyHold == true)
    {

        SQLiteDataBaseQueryHolder = "INSERT INTO AndroidJSonTable (name,phone_number) VALUES('"+NameHolder+"', '"+NumberHolder+"');";

        sqLiteDatabaseObj.execSQL(SQLiteDataBaseQueryHolder);

        Toast.makeText(MainActivity.this,"Data Inserted Successfully", Toast.LENGTH_LONG).show();

    }
    else {

        Toast.makeText(MainActivity.this,"Please Fill All The Required Fields.", Toast.LENGTH_LONG).show();

    }

}

 11. Clear the EditText after done insertion process.

public void EmptyEditTextAfterDataInsert(){

    editTextName.getText().clear();

    editTextPhoneNumber.getText().clear();

}

 12. Call all the 5 function inside the button click scope.

EnterData.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {


        SQLiteDataBaseBuild();

        SQLiteTableBuild();

        CheckEditTextStatus();

        InsertDataIntoSQLiteDatabase();

        EmptyEditTextAfterDataInsert();


    }
});

Final Code For Android SQLite Insert Data Into Database Tutorial :

Code for MainActivity.java file.

package com.androidjson.sqlitedatabasepart_1_androidjsoncom;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    SQLiteDatabase sqLiteDatabaseObj;
    EditText editTextName, editTextPhoneNumber;
    String NameHolder, NumberHolder, SQLiteDataBaseQueryHolder;
    Button EnterData;
    Boolean EditTextEmptyHold;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EnterData = (Button)findViewById(R.id.button);
        editTextName = (EditText)findViewById(R.id.editText);
        editTextPhoneNumber = (EditText)findViewById(R.id.editText2);

        EnterData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                SQLiteDataBaseBuild();

                SQLiteTableBuild();

                CheckEditTextStatus();

                InsertDataIntoSQLiteDatabase();

                EmptyEditTextAfterDataInsert();


            }
        });


    }

    public void SQLiteDataBaseBuild(){

        sqLiteDatabaseObj = openOrCreateDatabase("AndroidJSonDataBase", Context.MODE_PRIVATE, null);

    }

    public void SQLiteTableBuild(){

        sqLiteDatabaseObj.execSQL("CREATE TABLE IF NOT EXISTS AndroidJSonTable(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR, phone_number VARCHAR);");

    }

    public void CheckEditTextStatus(){

        NameHolder = editTextName.getText().toString() ;
        NumberHolder = editTextPhoneNumber.getText().toString();

        if(TextUtils.isEmpty(NameHolder) || TextUtils.isEmpty(NumberHolder)){

            EditTextEmptyHold = false ;

        }
        else {

            EditTextEmptyHold = true ;
        }
    }

    public void InsertDataIntoSQLiteDatabase(){

        if(EditTextEmptyHold == true)
        {

            SQLiteDataBaseQueryHolder = "INSERT INTO AndroidJSonTable (name,phone_number) VALUES('"+NameHolder+"', '"+NumberHolder+"');";

            sqLiteDatabaseObj.execSQL(SQLiteDataBaseQueryHolder);

            Toast.makeText(MainActivity.this,"Data Inserted Successfully", Toast.LENGTH_LONG).show();

        }
        else {

            Toast.makeText(MainActivity.this,"Please Fill All The Required Fields.", Toast.LENGTH_LONG).show();

        }

    }

    public void EmptyEditTextAfterDataInsert(){

        editTextName.getText().clear();

        editTextPhoneNumber.getText().clear();

    }

    }

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.androidjson.sqlitedatabasepart_1_androidjsoncom.MainActivity">

    <TextView
        android:text="SQLite Insert Into Database"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/textView"
        android:gravity="center"
        android:textSize="20dp"
        android:textColor="#000000"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Enter Name"
        android:ems="10"
        android:layout_marginTop="27dp"
        android:id="@+id/editText"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:gravity="center"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:hint="Enter Phone Number"
        android:gravity="center"
        android:ems="10"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:id="@+id/editText2" />

    <Button
        android:text="Insert Into SQLite Database"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:id="@+id/button" />
</RelativeLayout>

Screenshot:

SQLite Database

Download Code

Leave a Reply

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