<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schema" />

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schema" />

info@thistimebd.com

Thursday 21st of November 06:44:13pm

SQL Database android source code free download

Activity_main.xml

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



<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout 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"

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


    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Hello World!"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintRight_toRightOf="parent"

        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

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

mainActivity.java

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

package com.sam.web.sqlitdatabase;


import android.database.sqlite.SQLiteDatabase;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;


public class MainActivity extends AppCompatActivity {

    MyDatabaseHelper myDatabaseHelper;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        myDatabaseHelper=new MyDatabaseHelper(this);

        SQLiteDatabase sqLiteDatabase=myDatabaseHelper.getWritableDatabase();

    }

}

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

MyDatabaseHelper.java

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


package com.sam.web.sqlitdatabase;


import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.widget.Toast;


/**

 * Created by maudud on 1/30/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();




        }



    }

}