Monday, 4 May 2015

Storage On Android

Hi!

In Android to store the data, we are using the fallowing  format

On server :  SQL , MySQL & etc..,
On Local  : SharedPreferences & SQLite

Today i am going to cover about the SQLite. The SQLite is a lightweight library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.

Because of it's nature we are using the SQLite to store the data on the OS of ANDROID & IOS, etc..,

In Android we should extends the super class of SQLiteOpenHelper, the Helper class contains two superclass methods onCreate() & onUpgrade(). On the Oncreate method is used to create the tables, the  onUpgrade method is used to Update the tables.
For this class we should create one constructor which will carry the Database details to the superclass.

All the fields should be PRIVATE, STATIC & FINAL.

Android Code : 


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;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Selvaraj on 4/28/2015.
 */

public class Hotel_DB extends SQLiteOpenHelper
{

    private static final int Db_Version = 1;
    private static final String Db_Name = "hotelvgs3";

    private static final String Tb_booking = "Tb_booking";

    // Field for Room Booking
    final String Book_Rm_ID="IAvil_ID";

    final String Book_Rm_in_fl_date="Book_Rm_in_fl_date";
    final String Book_Rm_out_fl_date="Book_Rm_out_fl_date";
    final String Book_Rm_in_dt="Book_Rm_in_dt";
    final String Book_Rm_in_day="Book_Rm_in_day";
    final String Book_Rm_in_mnth="Book_Rm_in_mnth";
    final String Book_Rm_in_year="Book_Rm_in_year";
    final String Book_Rm_out_dt="Book_Rm_out_dt";
    final String Book_Rm_out_day="Book_Rm_out_day";
    final String Book_Rm_out_mnth="Book_Rm_out_mnth";
    final String Book_Rm_out_year="Book_Rm_out_year";

    final String Book_Rm_adlt="Book_Rm_adlt";
    final String Book_Rm_chld="Book_Rm_chld";

    final String Book_Rm_rom_typ="Book_Rm_rom_typ";
    final String Book_Rm_rom_rate="Book_Rm_rom_rate";
    final String Book_Rm_tot_rom="Book_Rm_tot_rom";

    final String Book_Rm_cust_fname="Book_Rm_cust_fname";
    final String Book_Rm_cust_lname="Book_Rm_cust_lname";
    final String Book_Rm_email="Book_Rm_email";
    final String Book_Rm_mob="Book_Rm_mob";
    final String Book_Rm_adrs="Book_Rm_adrs";
    final String Book_Rm_status="Book_Rm_status";

    final String Book_Rm_ack_tot="Book_Rm_ack_tot";
    final String Book_Rm_ack_advnc="Book_Rm_ack_advnc";






    public Hotel_DB(Context context) {
        super(context, Db_Name, null, Db_Version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_BOOKING_TABLE="CREATE TABLE " +Tb_booking+"("+Book_Rm_ID+" INTEGER PRIMARY KEY,"+Book_Rm_in_fl_date+" TEXT," +
                Book_Rm_out_fl_date+" TEXT,"+Book_Rm_in_dt+" TEXT,"+Book_Rm_in_day+" TEXT,"+Book_Rm_in_mnth+" TEXT,"+Book_Rm_in_year+
                " TEXT,"+Book_Rm_out_dt+" TEXT,"+Book_Rm_out_day+" TEXT,"+Book_Rm_out_mnth+" TEXT,"+Book_Rm_out_year+
                " TEXT,"+Book_Rm_adlt+" TEXT,"+Book_Rm_chld+" TEXT,"+Book_Rm_rom_typ+" TEXT,"+Book_Rm_rom_rate+" TEXT,"+Book_Rm_tot_rom+
                " TEXT,"+Book_Rm_cust_fname+" TEXT,"+Book_Rm_cust_lname+" TEXT,"+Book_Rm_email+" TEXT,"+Book_Rm_mob+" TEXT,"+Book_Rm_adrs+
                " TEXT,"+Book_Rm_status+" TEXT, "+Book_Rm_ack_tot+" TEXT, "+Book_Rm_ack_advnc+" TEXT "+")";
        
        db.execSQL(CREATE_BOOKING_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS"+Tb_booking);
        onCreate(db);
    }

    // Add Objects on the table
    public void addBooking(Booking_B Rpl_lst)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(Book_Rm_in_fl_date,Rpl_lst.getBook_Rm_in_fl_date());
        values.put(Book_Rm_out_fl_date,Rpl_lst.getBook_Rm_out_fl_date());
        values.put(Book_Rm_in_dt,Rpl_lst.getBook_Rm_in_dt());
        values.put(Book_Rm_in_day,Rpl_lst.getBook_Rm_in_day());
        values.put(Book_Rm_in_mnth,Rpl_lst.getBook_Rm_in_mnth());
        values.put(Book_Rm_in_year,Rpl_lst.getBook_Rm_in_year());
        values.put(Book_Rm_out_dt,Rpl_lst.getBook_Rm_out_dt());
        values.put(Book_Rm_out_day,Rpl_lst.getBook_Rm_out_day());
        values.put(Book_Rm_out_mnth,Rpl_lst.getBook_Rm_out_mnth());
        values.put(Book_Rm_out_year,Rpl_lst.getBook_Rm_out_year());
        values.put(Book_Rm_adlt,Rpl_lst.getBook_Rm_adlt());
        values.put(Book_Rm_chld,Rpl_lst.getBook_Rm_chld());
        values.put(Book_Rm_rom_typ,Rpl_lst.getBook_Rm_rom_typ());
        values.put(Book_Rm_rom_rate,Rpl_lst.getBook_Rm_rom_rate());
        values.put(Book_Rm_tot_rom,Rpl_lst.getBook_Rm_tot_rom());
        values.put(Book_Rm_cust_fname,Rpl_lst.getBook_Rm_cust_fname());
        values.put(Book_Rm_cust_lname,Rpl_lst.getBook_Rm_cust_lname());
        values.put(Book_Rm_email,Rpl_lst.getBook_Rm_email());
        values.put(Book_Rm_mob,Rpl_lst.getBook_Rm_mob());
        values.put(Book_Rm_adrs,Rpl_lst.getBook_Rm_adrs());
        values.put(Book_Rm_status,Rpl_lst.getBook_Rm_status());
        values.put(Book_Rm_ack_tot,Rpl_lst.getBook_Rm_ack_tot());
        values.put(Book_Rm_ack_advnc,Rpl_lst.getBook_Rm_ack_advnc());

        db.insert(Tb_booking, null, values);
        db.close();
        Log.d("BD Log", "EXECUTIVE SUCESS");
    }

    // Select list of all available records
    public ArrayList<Booking_B> get_All_Booking()
    {
        ArrayList<Booking_B> repay_list=new ArrayList<Booking_B>();
        String sltqry="SELECT * FROM "+ Tb_booking+" ORDER BY "+Book_Rm_cust_fname+" DESC";
        SQLiteDatabase db=this.getWritableDatabase();
        Cursor cursor=db.rawQuery(sltqry, null);
        if(cursor.moveToFirst())
        {
            do{
                Booking_B repy=new Booking_B();
                //exe.setId(String.valueOf(Integer.parseInt(cursor.getString(0))));
                repy.setBook_Rm_in_fl_date(cursor.getString(1));
                repy.setBook_Rm_out_fl_date(cursor.getString(2));
                repy.setBook_Rm_in_dt(cursor.getString(3));
                repy.setBook_Rm_in_day(cursor.getString(4));
                repy.setBook_Rm_in_mnth(cursor.getString(5));
                repy.setBook_Rm_in_year(cursor.getString(6));
                repy.setBook_Rm_out_dt(cursor.getString(7));
                repy.setBook_Rm_out_day(cursor.getString(8));
                repy.setBook_Rm_out_mnth(cursor.getString(9));
                repy.setBook_Rm_out_year(cursor.getString(10));
                repy.setBook_Rm_adlt(cursor.getString(11));
                repy.setBook_Rm_chld(cursor.getString(12));
                repy.setBook_Rm_rom_typ(cursor.getString(13));
                repy.setBook_Rm_rom_rate(cursor.getString(14));
                repy.setBook_Rm_tot_rom(cursor.getString(15));
                repy.setBook_Rm_cust_fname(cursor.getString(16));
                repy.setBook_Rm_cust_lname(cursor.getString(17));
                repy.setBook_Rm_email(cursor.getString(18));
                repy.setBook_Rm_mob(cursor.getString(19));
                repy.setBook_Rm_adrs(cursor.getString(20));
                repy.setBook_Rm_status(cursor.getString(21));
                repy.setBook_Rm_ack_tot(cursor.getString(22));
                repy.setBook_Rm_ack_advnc(cursor.getString(23));
                repay_list.add(repy);
            }
            while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return repay_list;
    }

    // Delete on Object from the database
    public void Delete_Booking(Booking_B contact) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(Tb_booking, Book_Rm_cust_fname + " = ?",
                new String[] { String.valueOf(contact.getBook_Rm_cust_fname()) });
        db.close();
    }

    
    // Select list of records by passing one parameter
    public List<Booking_B> get_Single_Bk_Detail_List(String fist_nm)
    {

        List<Booking_B> BBilllist=new ArrayList<Booking_B>();
        SQLiteDatabase db=this.getWritableDatabase();
        Cursor cursor=db.rawQuery("SELECT * FROM "+Tb_booking+" WHERE "+Book_Rm_cust_fname+" = ?",new String[] { fist_nm });
        if(cursor.moveToFirst())
        {
            do{
                Booking_B gn=new Booking_B();
                gn.setBook_Rm_cust_fname(cursor.getString(1));
                gn.setBook_Rm_cust_lname(cursor.getString(2));
                gn.setBook_Rm_mob(cursor.getString(3));
                gn.setBook_Rm_adrs(cursor.getString(4));
                BBilllist.add(gn);
            }
            while(cursor.moveToNext());
        }
        return BBilllist;

    }

    // Update Table By Objects

public int update_Plt_Detail(Mngr_Updt_B mngr_up_likst) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(price, mngr_up_likst.getPrice());
    values.put(status, mngr_up_likst.getStatus());

    return db.update(Tb_Plt_Detail, values, plat_number + " = ?",
            new String[]{String.valueOf(mngr_up_likst.getPlat_number())});
}

}


On The above code the "Booking_B" is the Business class for the Objects.

Lets Enjoy The Android..,

Thank You!

Have A Happy Day..,