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

Insert and View Data in SQLite Database (Android)

In this example i'm going to show you how we can insert and view 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.sqlitepractise.MainActivity">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter name" />

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Email" />

    <EditText
        android:id="@+id/contact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Contact" />

    <Button
        android:id="@+id/insert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:text="Insert" />

    <Button
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:text="View" />
</LinearLayout>



MainActivity.java


package com.example.malik.sqlitepractise;

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 {

    private EditText name, email, contact;
    private Button insert, view;

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

        insert = (Button) findViewById(R.id.insert);
        view = (Button) findViewById(R.id.view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ViewDataActivity.class);
                startActivity(intent);
            }
        });


        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.insertDataMethod(NAME,EMAIL,CONTACT,sqLiteDatabase);
                Toast.makeText(getApplicationContext(),"Data Inserted",Toast.LENGTH_LONG).show();
                databaseHelper.close();


                name.setText("");
                email.setText("");
                contact.setText("");


            }
        });

    }
}



DatabaseHelper.java


package com.example.malik.sqlitepractise;

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 14/12/2017.
 */

public class DatabaseHelper extends SQLiteOpenHelper {


    public static final String NAME = "name";
    public static final String EMAIl = "email";
    public static final String CONTACT = "contact";

    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("SQLite", "Database created");

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(QUERY_FOR_CREATE_TABLE);
        Log.d("SQLite", "Table created");
    }

    // for insert data
    public void insertDataMethod(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("SQLite", "Data inserted");

    }

    // for view data
    public Cursor getAllData(SQLiteDatabase sqLiteDatabase){
        Cursor cursor;
        String columns[] = {NAME,EMAIl,CONTACT};
        cursor = sqLiteDatabase.query(TABLE_NAME,columns,null,null,null,null,null);
        return cursor;
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}




Person.java


package com.example.malik.sqlitepractise;

/**
 * Created by malik on 14/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.sqlitepractise;

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 14/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.list_items_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;
    }
}


list_items_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="10dp"
        android:padding="10dp"
        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="10dp"
        android:padding="10dp"
        android:text="Email here"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/contact_here"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:padding="10dp"
        android:text="Contact here"
        android:textSize="20sp" />


</LinearLayout>



activity_view.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.sqlitepractise.ViewDataActivity">

    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>



ViewDataActivity.java


package com.example.malik.sqlitepractise;

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

        listView = (ListView) findViewById(R.id.list1);
        myAdapter = new MyAdapter(ViewDataActivity.this, R.layout.list_items_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());
        }

    }
}



AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.malik.sqlitepractise">

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