<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

Sunday 28th of April 11:00:53pm

SQL Database insert android app source code free download

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




</LinearLayout>

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

MainActivity.java

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



package com.sam.web.sqlitdatainsert;


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

    private EditText nameEditText,ageEditText,genderEditText;

    private Button addButton;

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

        addButton.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();




            }



        }


    }

}


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


MyDatabaseHelper.java

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


package com.sam.web.sqlitdatainsert;


import android.content.ContentValues;

import android.content.Context;

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="Student.db";

    private static final String TABLE_NAME="student_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 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;

    }

}