Create Simple Contact Demo in Sqlite Database with Insert,Update,Delete And Search Operation.In This Code to How to Create Database in Sqlite And How to Add Delete Update And Search Demo in Android.
Create Database Helper File to Create or Upgrade Database
DatabaseHelper.java
package com.example.bhaumik.sqldemo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Bhaumik on 5/29/2017.
*/
public class DatabaseHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME = "infos";
private static final String ID = "ID";
private static final String NAME = "NAME";
private static final String EMAIL = "EMAIL";
private static final String MOBILE_NO = "MOBILENO";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
+ NAME + " TEXT, " + EMAIL + " TEXT, " + MOBILE_NO + " TEXT );");
Log.d("Database : -> ","Database Create/Open...");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
}
public boolean InsertData(String name,String email,String mobile)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,name);
contentValues.put(EMAIL,email);
contentValues.put(MOBILE_NO,mobile);
long result = db.insert(TABLE_NAME,null,contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor SearchData(String mobile)
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE " + MOBILE_NO + " = '" + mobile + "'";
Cursor cursor = db.rawQuery(query,null);
return cursor;
}
public void UpdateData(String mobiles,String name,String email)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,name);
contentValues.put(EMAIL,email);
db.update(TABLE_NAME,contentValues,MOBILE_NO + " = '" + mobiles + "'",null);
}
public void DeleteData(String mobile)
{
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME , MOBILE_NO + " = '" + mobile + "'",null);
}
}
Create MainActivity File to Handle Add,Update,Delete And Search Event to Open Another Activity.
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorAccent"
tools:context="com.example.bhaumik.sqldemo.MainActivity">
<Button
android:id="@+id/btn_add_contact"
style="@style/ButtonStyle"
android:text="Add Contacts" />
<Button
android:id="@+id/btn_update_contact"
style="@style/ButtonStyle"
android:text="Update Contacts" />
<Button
android:id="@+id/btn_delete_contact"
style="@style/ButtonStyle"
android:text="Delete Contacts" />
<Button
android:id="@+id/btn_search_contact"
style="@style/ButtonStyle"
android:text="Search Contacts" />
</LinearLayout>
Create Code file to open Actiivity to Add,Update,Delete And Search Button Event.
MainActivity.Java
package com.example.bhaumik.sqldemo;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.crashlytics.android.Crashlytics;
import io.fabric.sdk.android.Fabric;
public class MainActivity extends AppCompatActivity{
Button add,update,delete,search;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHelper(this);
init();
setListener();
}
private void init() {
add = (Button) findViewById(R.id.btn_add_contact);
update = (Button) findViewById(R.id.btn_update_contact);
delete = (Button) findViewById(R.id.btn_delete_contact);
search = (Button) findViewById(R.id.btn_search_contact);
}
private void setListener() {
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,AddContactActivity.class);
startActivity(intent);
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,UpdateContactActivity.class);
startActivity(intent);
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,DeleteContactActivity.class);
startActivity(intent);
}
});
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SearchContactActivity.class);
startActivity(intent );
}
});
}
}
Design For Add,Update,Delete And Search Activity XMl File.To Open Activity when button click event.
addcontact_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimary"
tools:context="com.example.bhaumik.sqldemo.AddContactActivity">
<EditText
android:id="@+id/et_name"
style="@style/EditTextStyle"
android:textColorHint="@color/WhiteColor"
android:hint="Enter Name"/>
<EditText
android:id="@+id/et_email"
style="@style/EditTextStyle"
android:textColorHint="@color/WhiteColor"
android:hint="Enter Email"/>
<EditText
android:id="@+id/et_mobile"
style="@style/EditTextStyle"
android:hint="Enter Mobile No"
android:inputType="phone"
android:textColorHint="@color/WhiteColor"
android:imeOptions="actionDone"/>
<Button
android:id="@+id/btn_add"
android:layout_width="250dp"
android:layout_height="50dp"
android:text="Add "
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="@color/ButtonColor"
android:textColor="@color/WhiteColor"
android:textSize="18dp"
/>
</LinearLayout>
updatecontact_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bhaumik.sqldemo.UpdateContactActivity">
<EditText
style="@style/EditTextStyle"
android:id="@+id/et_update_mobile"
android:hint="Enter Mobile No"
android:inputType="phone"
android:textColor="@color/ButtonColor"/>
<Button
style="@style/ButtonStyle"
android:id="@+id/btn_search_updates"
android:text="Search"/>
<EditText
style="@style/EditTextStyle"
android:id="@+id/et_update_name"
android:hint="Your Name"
android:textColor="@color/ButtonColor"/>
<EditText
style="@style/EditTextStyle"
android:id="@+id/et_update_email"
android:hint="Your Email"
android:textColor="@color/ButtonColor"/>
<Button
style="@style/ButtonStyle"
android:id="@+id/btn_update"
android:text="Update"/>
</LinearLayout>
deletecontact_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bhaumik.sqldemo.DeleteContactActivity">
<EditText
style="@style/EditTextStyle"
android:id="@+id/et_delete_mobile"
android:textColor="@color/ButtonColor"/>
<Button
style="@style/ButtonStyle"
android:id="@+id/btn_delete"
android:text="Delete"/>
</LinearLayout>
searchcontact_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.bhaumik.sqldemo.SearchContactActivity">
<EditText
style="@style/EditTextStyle"
android:id="@+id/et_search_mobile"
android:hint="Enter Mobile No"
android:textColor="@color/ButtonColor"
android:inputType="phone"
/>
<Button
style="@style/ButtonStyle"
android:id="@+id/btn_search"
android:text="Search"/>
<TextView
style="@style/TextViewStyle"
android:id="@+id/tv_search_name"
/>
<TextView
style="@style/TextViewStyle"
android:id="@+id/tv_search_email"/>
</LinearLayout>
Create Java Code File to Add,Update,Delete And Search Activity.
AddContactActivity.java
package com.example.bhaumik.sqldemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddContactActivity extends AppCompatActivity {
EditText name,email,mobile;
Button add;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
databaseHelper = new DatabaseHelper(this);
init();
setListener();
}
private void setListener() {
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean result = databaseHelper.InsertData(name.getText().toString(),email.getText().toString(),mobile.getText().toString());
if(result)
{
Toast.makeText(AddContactActivity.this,"Contact Inserted Successfully...",Toast.LENGTH_SHORT).show();
name.setText("");
email.setText("");
mobile.setText("");
}
else
{
Toast.makeText(AddContactActivity.this,"Failed Contact Inserted...",Toast.LENGTH_SHORT).show();
}
}
});
}
private void init() {
name = (EditText) findViewById(R.id.et_name);
email = (EditText) findViewById(R.id.et_email);
mobile = (EditText) findViewById(R.id.et_mobile);
add = (Button) findViewById(R.id.btn_add);
}
}
UpdateContactActivity.java
package com.example.bhaumik.sqldemo;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class UpdateContactActivity extends AppCompatActivity {
EditText mobile,name,email;
Button search,update;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_contact);
databaseHelper = new DatabaseHelper(this);
init();
setListener();
}
private void setListener() {
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = databaseHelper.SearchData(mobile.getText().toString());
while (cursor.moveToNext())
{
name.setText(cursor.getString(1));
email.setText(cursor.getString(2));
}
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
databaseHelper.UpdateData(mobile.getText().toString(),name.getText().toString(),email.getText().toString());
Toast.makeText(UpdateContactActivity.this,"Contact Updated Successfully..",Toast.LENGTH_SHORT).show();
mobile.setText("");
name.setText("");
email.setText("");
}
});
}
private void init() {
mobile = (EditText) findViewById(R.id.et_update_mobile);
name = (EditText) findViewById(R.id.et_update_name);
email = (EditText) findViewById(R.id.et_update_email);
search = (Button) findViewById(R.id.btn_search_updates);
update = (Button) findViewById(R.id.btn_update);
}
}
DeleteContactActivity.java
package com.example.bhaumik.sqldemo;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class UpdateContactActivity extends AppCompatActivity {
EditText mobile,name,email;
Button search,update;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_contact);
databaseHelper = new DatabaseHelper(this);
init();
setListener();
}
private void setListener() {
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = databaseHelper.SearchData(mobile.getText().toString());
while (cursor.moveToNext())
{
name.setText(cursor.getString(1));
email.setText(cursor.getString(2));
}
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
databaseHelper.UpdateData(mobile.getText().toString(),name.getText().toString(),email.getText().toString());
Toast.makeText(UpdateContactActivity.this,"Contact Updated Successfully..",Toast.LENGTH_SHORT).show();
mobile.setText("");
name.setText("");
email.setText("");
}
});
}
private void init() {
mobile = (EditText) findViewById(R.id.et_update_mobile);
name = (EditText) findViewById(R.id.et_update_name);
email = (EditText) findViewById(R.id.et_update_email);
search = (Button) findViewById(R.id.btn_search_updates);
update = (Button) findViewById(R.id.btn_update);
}
}
SearchContactActivity.java
package com.example.bhaumik.sqldemo;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SearchContactActivity extends AppCompatActivity {
EditText mobile;
TextView name,email;
Button search;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_contact);
databaseHelper = new DatabaseHelper(this);
init();
setListener();
}
private void setListener() {
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = databaseHelper.SearchData(mobile.getText().toString());
while (cursor.moveToNext())
{
name.setText(cursor.getString(1));
email.setText(cursor.getString(2));
}
}
});
}
private void init() {
mobile = (EditText) findViewById(R.id.et_search_mobile);
name = (TextView) findViewById(R.id.tv_search_name);
email = (TextView) findViewById(R.id.tv_search_email);
search = (Button) findViewById(R.id.btn_search);
}
}