Here you'll get help for all programming languages like android,php, html, css, bootstrap, javaScript, java etc...

Insert, View & Search Data in SQLite Database (Android)

In this example we are going to learn how to Insert, View & Search 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" />
</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;
    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);
            }
        });

        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;
    }

    @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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search name"
        android:id="@+id/searchName"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search"
        android:id="@+id/searchButton"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email here"
        android:id="@+id/searchEmail"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Contact here"
        android:id="@+id/searchContact"
        />

</LinearLayout>



SearchContactActivity.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.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SearchContactActivity extends AppCompatActivity {

    EditText searchName, searchEmail, searchContact;
    Button searchButton;

    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);

        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());
                }
            }
        });

    }
}



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

</manifest>

User Help

If you want any source code or any other help just contact me at malik_himani@ymail.com. Your requirement's code will be uploaded in blog soon.