Thursday, September 24, 2015

Android Database TestAdapter file

package com.example;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TestAdapter {
    protected static final String TAG = "DataAdapter";

    private final Context mContext;
    private SQLiteDatabase mDb;
    private DataBaseHelper mDbHelper;

    public TestAdapter(Context context) {
        this.mContext = context;
        mDbHelper = new DataBaseHelper(mContext);
    }

    public TestAdapter createDatabase() throws SQLException {
        try {
            mDbHelper.createDataBase();
        } catch (IOException mIOException) {
            Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
            throw new Error("UnableToCreateDatabase");
        }
        return this;
    }

    public TestAdapter open() throws SQLException {
        try {
            mDbHelper.openDataBase();
            mDbHelper.close();
            mDb = mDbHelper.getReadableDatabase();
        } catch (SQLException mSQLException) {
            Log.e(TAG, "open >>" + mSQLException.toString());
            throw mSQLException;
        }
        return this;
    }

    public void close() {
        mDbHelper.close();
    }

    public Cursor getTestData() {
        try {
            String sql = "SELECT manager_name FROm manager where manager_id=1";

            Cursor mCur = mDb.rawQuery(sql, null);
            if (mCur != null) {
                mCur.moveToNext();

            }
            return mCur;
        } catch (SQLException mSQLException) {
            Log.e(TAG, "getTestData >>" + mSQLException.toString());
            throw mSQLException;
        }
    }

    public Cursor getTaxData() {
        try {
            String sql = "SELECT service_tax,vat_tax  FROm tax where tax_id=1";

            Cursor mCur = mDb.rawQuery(sql, null);
            if (mCur != null) {
                mCur.moveToNext();

            }
            return mCur;
        } catch (SQLException mSQLException) {
            Log.e(TAG, "getTestData >>" + mSQLException.toString());
            throw mSQLException;
        }
    }

    public Cursor getStoreData() {
        try {
            String sql = "SELECT store_name FROm store where store_id=1";

            Cursor mCur = mDb.rawQuery(sql, null);
            if (mCur != null) {
                mCur.moveToNext();

            }
            return mCur;
        } catch (SQLException mSQLException) {
            Log.e(TAG, "getTestData >>" + mSQLException.toString());
            throw mSQLException;
        }
    }

    public Cursor selectQuery(String query) {
        Cursor c1 = null;
        try {

            if (mDb.isOpen()) {
                mDb.close();

            }
            mDb = mDbHelper.getWritableDatabase();
            c1 = mDb.rawQuery(query, null);

        } catch (Exception e) {

            System.out.println("DATABASE ERROR " + e);

        }
        return c1;

    }

    public void executeQuery(String query) {
        try {

            if (mDb.isOpen()) {
                mDb.close();
            }

            mDb = mDbHelper.getWritableDatabase();
            mDb.execSQL(query);

        } catch (Exception e) {

            System.out.println("DATABASE ERROR " + e);
        }
    }

    public void deleteQuery(String query) {
        try {

            if (mDb.isOpen()) {
                mDb.close();
            }

            mDb = mDbHelper.getWritableDatabase();
            mDb.execSQL(query);

        } catch (Exception e) {

            System.out.println("DATABASE ERROR " + e);
        }

    }

    public void updateQuery(String query) {
        try {

            if (mDb.isOpen()) {
                mDb.close();
            }

            mDb = mDbHelper.getWritableDatabase();
            mDb.execSQL(query);

        } catch (Exception e) {

            System.out.println("DATABASE ERROR " + e);
        }

    }

    public boolean SaveEmployee(String name) {
        try {
            ContentValues cv = new ContentValues();
            cv.put("category_name", name);
            // cv.put("Email", email);

            mDb.insert("category", null, cv);

            Log.d("Save category", "informationsaved");
            return true;

        } catch (Exception ex) {
            Log.d("SaveCategory", ex.toString());
            return false;
        }
    }

    public List<String> getAllLabels() {
        List<String> labels = new ArrayList<String>();
        String selectQuery = "SELECT  * FROM  category";
        mDb = mDbHelper.getWritableDatabase();
        Cursor cursor = mDb.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                labels.add(cursor.getString(1));
            } while (cursor.moveToNext());
        }

        // closing connection
        cursor.close();
        mDb.close();

        // returning lables
        return labels;
    }
    public Cursor getMrpData(String id)
    {
        try {
            String sql="select product_mrp,product_quantity from product where product_id='"+id+"'";
            Cursor mCur = mDb.rawQuery(sql, null);
            if (mCur != null) {
                mCur.moveToNext();

            }
            return mCur;
        } catch (SQLException mSQLException) {
            Log.e(TAG, "getTestData >>" + mSQLException.toString());
            throw mSQLException;
        }
    }
   

}

No comments:

Post a Comment