In this example we are going to learn that how to Insert, View, Search, Delete & Update Data in SQLite Database in Android.
activity_main.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:padding="10dp"
tools:context="com.example.malik.malikhimani.MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email" />
<EditText
android:id="@+id/contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Contact" />
<Button
android:id="@+id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Insert" />
<Button
android:id="@+id/viewData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="View Data" />
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Search" />
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Update" />
</LinearLayout>
activity_main.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:padding="10dp"
tools:context="com.example.malik.malikhimani.MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email" />
<EditText
android:id="@+id/contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Contact" />
<Button
android:id="@+id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Insert" />
<Button
android:id="@+id/viewData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="View Data" />
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Search" />
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Update" />
</LinearLayout>
MainActivity.java
package com.example.malik.malikhimani;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
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 MainActivity extends AppCompatActivity {
EditText name, email, contact;
Button insert, viewData, search, update;
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.name);
email = (EditText) findViewById(R.id.email);
contact = (EditText) findViewById(R.id.contact);
viewData = (Button) findViewById(R.id.viewData);
viewData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ViewDataActivity.class);
startActivity(intent);
}
});
search = (Button) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SearchContactActivity.class);
startActivity(intent);
}
});
update = (Button) findViewById(R.id.update);
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, UpdateContactActivity.class);
startActivity(intent);
}
});
insert = (Button) findViewById(R.id.insert);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String NAME = name.getText().toString();
String EMAIL = email.getText().toString();
String CONTACT = contact.getText().toString();
databaseHelper = new DatabaseHelper(MainActivity.this);
sqLiteDatabase = databaseHelper.getWritableDatabase();
databaseHelper.insertData(NAME,EMAIL,CONTACT,sqLiteDatabase);
Toast.makeText(getApplicationContext(),"Data inserted",Toast.LENGTH_LONG).show();
databaseHelper.close();
}
});
}
}
Person.java
package com.example.malik.malikhimani;
/**
* Created by malik on 17/12/2017.
*/
public class Person {
String name;
String email;
String contact;
public Person() {
}
public Person(String name, String email, String contact) {
this.name = name;
this.email = email;
this.contact = contact;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
}
MyAdapter.java
package com.example.malik.malikhimani;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by malik on 17/12/2017.
*/
public class MyAdapter extends ArrayAdapter {
List list = new ArrayList();
public MyAdapter(@NonNull Context context, @LayoutRes int resource) {
super(context, resource);
}
@Override
public void add(@Nullable Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return list.get(position);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_layout, null);
TextView name = (TextView) convertView.findViewById(R.id.name_here);
TextView email = (TextView) convertView.findViewById(R.id.email_here);
TextView contact = (TextView) convertView.findViewById(R.id.contact_here);
Person person = (Person) list.get(position);
name.setText(person.getName());
email.setText(person.getEmail());
contact.setText(person.getContact());
return convertView;
}
}
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/name_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:padding="5dp"
android:text="Name here"
android:textSize="25sp" />
<TextView
android:id="@+id/email_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:padding="5dp"
android:text="Email here"
android:textSize="25sp" />
<TextView
android:id="@+id/contact_here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:padding="5dp"
android:text="Contact here"
android:textSize="25sp" />
</LinearLayout>
DatabaseHelper.java
package com.example.malik.malikhimani;
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;
/**
* Created by malik on 15/12/2017.
*/
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String NAME = "nameColumn";
public static final String EMAIL = "emailColumn";
public static final String CONTACT = "contactColumn";
public static final String TABLE_NAME = "personTable";
public static final String DATABASE_NAME = "personDatabase";
public static final int DATABASE_VERSION = 1;
public static final String QUERY_FOR_CREATE_TABLE =
"create table "+TABLE_NAME+"("+NAME+" text,"+EMAIL+" text,"+CONTACT+" text);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("DATABASE", "Database created");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(QUERY_FOR_CREATE_TABLE);
Log.d("DATABASE", "Table created");
}
// method for insert data into sqlite databse
public void insertData(String name, String email, String contact, SQLiteDatabase sqLiteDatabase){
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,name);
contentValues.put(EMAIL,email);
contentValues.put(CONTACT,contact);
sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
Log.d("DATABASE", "Data inserted");
}
// view data method Curso
public Cursor getAllData(SQLiteDatabase sqLiteDatabase){
Cursor cursor;
String column[] = {NAME,EMAIL,CONTACT};
cursor = sqLiteDatabase.query(TABLE_NAME,column, null,null,null,null,null);
return cursor;
}
// search method
public Cursor searchContacts(String name, SQLiteDatabase sqLiteDatabase){
Cursor cursor;
String column[] = {EMAIL, CONTACT};
String searchQry = NAME + " LIKE ?";
String string[] = {name};
cursor = sqLiteDatabase.query(TABLE_NAME,column,searchQry,string,null,null,null);
return cursor;
}
//delete
public void deleteContactDetails(String name, SQLiteDatabase sqLiteDatabase){
String deleteQry = NAME + " LIKE ?";
String string[] = {name};
sqLiteDatabase.delete(TABLE_NAME,deleteQry,string);
}
// update
public void updateContacts(String p_name, String n_name, String n_email, String n_contact, SQLiteDatabase sqLiteDatabase){
ContentValues contentValues = new ContentValues();
contentValues.put(NAME,n_name);
contentValues.put(EMAIL,n_email);
contentValues.put(CONTACT,n_contact);
String updateQry = NAME + " LIKE ?";
String string[] = {p_name};
sqLiteDatabase.update(TABLE_NAME,contentValues,updateQry,string);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
activity_view_data.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.malik.malikhimani.ViewDataActivity">
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
ViewDataActivity.java
package com.example.malik.malikhimani;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
public class ViewDataActivity extends AppCompatActivity {
ListView listView;
MyAdapter myAdapter;
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_data);
listView = (ListView) findViewById(R.id.list1);
myAdapter = new MyAdapter(ViewDataActivity.this, R.layout.custom_layout);
listView.setAdapter(myAdapter);
databaseHelper = new DatabaseHelper(ViewDataActivity.this);
sqLiteDatabase = databaseHelper.getReadableDatabase();
cursor = databaseHelper.getAllData(sqLiteDatabase);
if(cursor.moveToFirst()){
do{
String name, email, contact;
name = cursor.getString(0);
email = cursor.getString(1);
contact = cursor.getString(2);
Person person = new Person(name,email,contact);
myAdapter.add(person);
} while (cursor.moveToNext());
}
}
}
activity_search_contact.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:padding="10dp"
tools:context="com.example.malik.malikhimani.SearchContactActivity">
<EditText
android:id="@+id/searchName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search name" />
<Button
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
android:text="Search" />
<EditText
android:id="@+id/searchEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email here" />
<EditText
android:id="@+id/searchContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Contact here" />
<Button
android:id="@+id/deleteButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
android:text="Delete Contact" />
</LinearLayout>
SearchContactActivity.java
package com.example.malik.malikhimani;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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 SearchContactActivity extends AppCompatActivity {
EditText searchName, searchEmail, searchContact;
Button searchButton, deleteButton;
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
String NAME;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_contact);
searchName = (EditText) findViewById(R.id.searchName);
searchEmail = (EditText) findViewById(R.id.searchEmail);
searchContact = (EditText) findViewById(R.id.searchContact);
searchButton = (Button) findViewById(R.id.searchButton);
deleteButton = (Button) findViewById(R.id.deleteButton1);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NAME = searchName.getText().toString();
databaseHelper = new DatabaseHelper(SearchContactActivity.this);
sqLiteDatabase = databaseHelper.getReadableDatabase();
cursor = databaseHelper.searchContacts(NAME,sqLiteDatabase);
if(cursor.moveToFirst()){
do{
String email, contact;
email = cursor.getString(0);
contact = cursor.getString(1);
searchEmail.setText(email);
searchContact.setText(contact);
} while (cursor.moveToNext());
}
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
databaseHelper = new DatabaseHelper(SearchContactActivity.this);
sqLiteDatabase = databaseHelper.getReadableDatabase();
databaseHelper.deleteContactDetails(NAME,sqLiteDatabase);
Toast.makeText(getApplicationContext(), "Contact deleted",Toast.LENGTH_LONG).show();
finish();
Intent intent = new Intent(SearchContactActivity.this, ViewDataActivity.class);
startActivity(intent);
}
});
}
}
activity_update_contact.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:padding="10dp"
tools:context="com.example.malik.malikhimani.UpdateContactActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="35dp"
android:orientation="horizontal">
<EditText
android:id="@+id/upEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Search " />
<Button
android:id="@+id/upButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search" />
</LinearLayout>
<EditText
android:id="@+id/uName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name here" />
<EditText
android:id="@+id/uEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email here" />
<EditText
android:id="@+id/uContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Contact here" />
<Button
android:id="@+id/uButtonUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Update contact" />
</LinearLayout>
UpdateContactActivity.java
package com.example.malik.malikhimani;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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 editText;
Button searchButton;
EditText name, email, contact;
Button updateBUtton;
DatabaseHelper databaseHelper;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
String NAME;
String getName, getEmail, getContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_contact);
editText = (EditText) findViewById(R.id.upEditText);
searchButton = (Button) findViewById(R.id.upButton);
name = (EditText) findViewById(R.id.uName);
email = (EditText) findViewById(R.id.uEmail);
contact = (EditText) findViewById(R.id.uContact);
updateBUtton = (Button) findViewById(R.id.uButtonUpdate);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NAME = editText.getText().toString();
databaseHelper = new DatabaseHelper(UpdateContactActivity.this);
sqLiteDatabase = databaseHelper.getReadableDatabase();
cursor = databaseHelper.searchContacts(NAME,sqLiteDatabase);
if(cursor.moveToFirst()){
do{
String email1, contact1;
email1 = cursor.getString(0);
contact1 = cursor.getString(1);
getName = NAME;
getEmail = email1;
getContact = contact1;
name.setText(getName);
email.setText(getEmail);
contact.setText(getContact);
} while (cursor.moveToNext());
}
}
});
updateBUtton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String NAME1, EMAIL1, CONTACT1;
NAME1 = name.getText().toString();
EMAIL1 = email.getText().toString();
CONTACT1 = contact.getText().toString();
databaseHelper = new DatabaseHelper(UpdateContactActivity.this);
sqLiteDatabase = databaseHelper.getWritableDatabase();
databaseHelper.updateContacts(NAME,NAME1,EMAIL1,CONTACT1,sqLiteDatabase);
Toast.makeText(getApplicationContext(),"Contact updated",Toast.LENGTH_LONG).show();
finish();
Intent intent = new Intent(UpdateContactActivity.this, ViewDataActivity.class);
startActivity(intent);
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.malik.malikhimani">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ViewDataActivity" />
<activity android:name=".SearchContactActivity" />
<activity android:name=".UpdateContactActivity"></activity>
</application>
</manifest>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.malik.malikhimani"
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}