Insert data in SQLite database in android using EditText example with source code

How to submit EditText entered values directly into SQLite db tables on button click in android.

In our previous tutorial we have learned about Basic structure of SQLite database and Tables and how can we  create db + tables inside android activity, Now we are going to move on the next advance chapter of SQLite Database tutorial. In this tutorial we are going to submit values inside SQLite tables(which we have already designed in our previous tutorial). This is tutorial is advance then others because we are inserting EditText typed values into SQLite tables so this method is completely dynamic + User friendly. So here is the complete step by step tutorial for Insert data in SQLite database in android using EditText example with source code.

android-project-download-code-button

How to Insert data in SQLite database in android using EditText example with source code.

Code for MainActivity.java file.

 package com.insertdatainsqlitedatabase_android_examples.com;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
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 Activity {

 EditText GetName,GetPhoneNumber,GetSubject ;
 Button Submit ;
 String Name, PhoneNumber, Subject ;
 Boolean CheckEditTextEmpty ;
 String SQLiteQuery;
 SQLiteDatabase SQLITEDATABASE;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);GetName = (EditText)findViewById(R.id.editText1);
 
 GetPhoneNumber = (EditText)findViewById(R.id.editText2);
 
 GetSubject = (EditText)findViewById(R.id.editText3);
 
 Submit = (Button)findViewById(R.id.button1);
 
 Submit.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 DBCreate();
 
 SubmitData2SQLiteDB();
 
 }
 });
 
 }
 
 public void DBCreate(){
 
 SQLITEDATABASE = openOrCreateDatabase("DemoDataBase", Context.MODE_PRIVATE, null);
 
 SQLITEDATABASE.execSQL("CREATE TABLE IF NOT EXISTS demoTable(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR, phone_number VARCHAR, subject VARCHAR);");
 }
 
 public void SubmitData2SQLiteDB(){
 
 Name = GetName.getText().toString();
 
 PhoneNumber = GetPhoneNumber.getText().toString();
 
 Subject = GetSubject.getText().toString();
 
 CheckEditTextIsEmptyOrNot( Name,PhoneNumber, Subject);
 
 if(CheckEditTextEmpty == true)
 {
 
 SQLiteQuery = "INSERT INTO demoTable (name,phone_number,subject) VALUES('"+Name+"', '"+PhoneNumber+"', '"+Subject+"');";
 
 SQLITEDATABASE.execSQL(SQLiteQuery);
 
 Toast.makeText(MainActivity.this,"Data Submit Successfully", Toast.LENGTH_LONG).show(); 
 
 ClearEditTextAfterDoneTask();
 
 }
 else {
 
 Toast.makeText(MainActivity.this,"Please Fill All the Fields", Toast.LENGTH_LONG).show();
 }
 }
 
 public void CheckEditTextIsEmptyOrNot(String Name,String PhoneNumber, String subject ){
 
 if(TextUtils.isEmpty(Name) || TextUtils.isEmpty(PhoneNumber) || TextUtils.isEmpty(Subject)){
 
 CheckEditTextEmpty = false ;
 
 }
 else {
 CheckEditTextEmpty = true ;
 }
 }
 
 public void ClearEditTextAfterDoneTask(){
 
 GetName.getText().clear();
 GetPhoneNumber.getText().clear();
 GetSubject.getText().clear();
 
 }
 
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 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.insertdatainsqlitedatabase_android_examples.com.MainActivity" >

 <EditText
 android:id="@+id/editText1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:ems="10"
 android:hint="Enter Name Here"
 android:gravity="center" />

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

 <EditText
 android:id="@+id/editText3"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/editText2"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="20dp"
 android:ems="10" 
 android:hint="Enter Subject Here"
 android:gravity="center" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click Here to Insert Data into SQLite Database" />

</RelativeLayout>

Screenshots:

Insert data in SQLite database in android using EditText example with source code

 

done-sqlite

Click here to download Insert data in SQLite database in android using EditText example with source code project with source code.

Leave a Reply

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