<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"

    a" />

<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"

    a" />

info@thistimebd.com

Tuesday 3rd of December 11:40:59pm

SQLite Read Data Android app source code

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"

    tools:context="com.sam.web.sqlitereaddata.MainActivity">


    <LinearLayout

        android:layout_margin="10dp"

        android:orientation="vertical"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">


        <TextView



            android:text="Name"


            android:textStyle="bold"

            android:textSize="20sp"


            android:layout_width="match_parent"

            android:layout_height="wrap_content" />


        <EditText

            android:id="@+id/nameEditTextId"

            android:textSize="20sp"

            android:textStyle="bold"

            android:layout_width="match_parent"

            android:layout_height="wrap_content" />


    </LinearLayout>


    <LinearLayout

        android:layout_margin="10dp"

        android:orientation="vertical"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">


        <TextView



            android:text="Age"


            android:textStyle="bold"

            android:textSize="20sp"


            android:layout_width="match_parent"

            android:layout_height="wrap_content" />


        <EditText

            android:id="@+id/ageEditTextId"

            android:textSize="20sp"

            android:textStyle="bold"

            android:layout_width="match_parent"

            android:layout_height="wrap_content" />



    </LinearLayout>


    <LinearLayout

        android:layout_margin="10dp"

        android:orientation="vertical"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">


        <TextView



            android:text="Gender"


            android:textStyle="bold"

            android:textSize="20sp"


            android:layout_width="match_parent"

            android:layout_height="wrap_content" />


        <EditText

            android:id="@+id/genderEditTextId"

            android:textSize="20sp"

            android:textStyle="bold"

            android:layout_width="match_parent"

            android:layout_height="wrap_content" />



    </LinearLayout>


    <Button

        style="@style/Widget.AppCompat.Button.Colored"

        android:text="ADD Data"

        android:id="@+id/addButtonId"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />


    <Button

        style="@style/Widget.AppCompat.Button.Colored"

        android:text="Display all Data"

        android:id="@+id/displayAllDataButtonId"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />



</LinearLayout>

===================================================================================

MainActivity.java

===================

package com.sam.web.sqlitereaddata;


import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.support.v7.app.AlertDialog;

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 implements View.OnClickListener{

    private EditText nameEditText,ageEditText,genderEditText;

    private Button addButton,displayAllDataButton;

    MyDatabaseHelper myDatabaseHelper;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        myDatabaseHelper=new MyDatabaseHelper(this);

        SQLiteDatabase sqLiteDatabase=myDatabaseHelper.getWritableDatabase();

        nameEditText=(EditText) findViewById(R.id.nameEditTextId);

        ageEditText=(EditText) findViewById(R.id.ageEditTextId);

        genderEditText=(EditText) findViewById(R.id.genderEditTextId);

        addButton=(Button) findViewById(R.id.addButtonId);

        displayAllDataButton=(Button) findViewById(R.id.displayAllDataButtonId);

        addButton.setOnClickListener(this);

        displayAllDataButton.setOnClickListener(this);

    }


    @Override

    public void onClick(View view) {

        String name=nameEditText.getText().toString();

        String age=ageEditText.getText().toString();

        String gender=genderEditText.getText().toString();


        if(view.getId()==R.id.addButtonId){

            long rowId=myDatabaseHelper.insertData(name,age,gender);

            if (rowId==1){

                Toast.makeText(getApplicationContext(),"unsuccessfull",Toast.LENGTH_LONG).show();




            }else {

                Toast.makeText(getApplicationContext(),"Row "+rowId+" is successfully inserted",Toast.LENGTH_LONG).show();




            }



        }

        if (view.getId()==R.id.displayAllDataButtonId){


            Cursor cursor=myDatabaseHelper.displayAllData();

            if(cursor.getCount()==0){


                //if no data will be display message

                showData("Error","No data found");

                return;

            }

            StringBuffer stringBuffer=new StringBuffer();

            while (cursor.moveToNext()){


                stringBuffer.append("ID :"+cursor.getString(0)+" ");

                stringBuffer.append("Name :"+cursor.getString(1)+" ");

                stringBuffer.append("Age :"+cursor.getString(2)+" ");

                stringBuffer.append("Gender :"+cursor.getString(3)+" ");

            }

            showData("resultSet",stringBuffer.toString());




        }


    }

    public void showData(String title,String message){



        AlertDialog.Builder builder=new AlertDialog.Builder(this);

        builder.setTitle(title);

        builder.setMessage(message);

        builder.setCancelable(true);

        builder.show();

    }

}

====================================================================================

MyDatabaseHelper.java

=====================


package com.sam.web.sqlitereaddata;


import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.widget.Toast;


/**

 * Created by maudud on 1/31/18.

 */



public class MyDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME="Student1.db";

    private static final String TABLE_NAME="student1_details";

    private static final String ID="_id";

    private static final String NAME="Name";

    private static final String AGE="Age";

    private static final String GENDER="Gender";

    private static final int VERSION_NUMBER=1;

    private static final String CREATE_TABLE= "CREATE TABLE "+TABLE_NAME+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME+" VARCHAR(255),"+AGE+" INTEGER, "+GENDER+" VARCHAR(15));";

    private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME;

    private static final String SELECT_ALL="SELECT * FROM"+TABLE_NAME;

    private Context context;


    public MyDatabaseHelper(Context context) {

        super(context, DATABASE_NAME, null, VERSION_NUMBER);

        this.context=context;

    }


    @Override

    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        try {

            Toast.makeText(context," onCreate is called",Toast.LENGTH_LONG).show();



            sqLiteDatabase.execSQL(CREATE_TABLE);


        }catch (Exception e){

            Toast.makeText(context,"Exception :"+e,Toast.LENGTH_LONG).show();



        }




    }


    @Override

    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

        try {

            Toast.makeText(context," onUpgrade is called",Toast.LENGTH_LONG).show();


            sqLiteDatabase.execSQL(DROP_TABLE);

            onCreate(sqLiteDatabase);



        }catch (Exception e){

            Toast.makeText(context,"Exception :"+e,Toast.LENGTH_LONG).show();




        }



    }

    public long insertData(String name,String age, String gender){



       SQLiteDatabase sqLiteDatabase= this.getWritableDatabase();

       ContentValues contentValues=new ContentValues();

       contentValues.put(NAME,name);

       contentValues.put(AGE,age);

       contentValues.put(GENDER,gender);

       long rowId=sqLiteDatabase.insert(TABLE_NAME,null,contentValues);

       return rowId;

    }

    public Cursor displayAllData(){


        SQLiteDatabase sqLiteDatabase= this.getWritableDatabase();

        Cursor cursor=sqLiteDatabase.rawQuery(SELECT_ALL,null);

        return cursor;





    }

}