Android Open Source - DisposableIncome-OldJava Database Helper






From Project

Back to project page DisposableIncome-OldJava.

License

The source code is released under:

MIT License

If you think the Android project DisposableIncome-OldJava listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package uk.co.wilka.disposableincome;
/*from  w  w  w  . ja  v a 2  s. c om*/
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;

import java.util.ArrayList;
import java.util.Date;


public class DatabaseHelper extends SQLiteOpenHelper implements IAccessData {

    private static class WithdrawTable {
        public static final String TableName = "withdraw_table";
        public static final String AmountColumn = "amount_pence";
        public static final String NotesColumn = "notes";
        public static final String DateColumn = "date";
        public static final String TypeColumn = "type";
        public static final String IdColumn = "_id";
    }

    private static class OptionsTable {
        public static final String TableName = "options_table";
        public static final String IdColumn = "_id";
        public static final String OptionNameColumn = "option";
        public static final String ValueColumn = "option_value";
    }

    private static class OptionKeyNames {
        public static final String MonthlyBudget  = "MonthlyBudget";
    }

    private static final String DatabaseName = "disposable-income-db";

    public DatabaseHelper(Context context) {
        super(context, DatabaseName, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + WithdrawTable.TableName + " (" + WithdrawTable.IdColumn + " INTEGER PRIMARY KEY AUTOINCREMENT, " + WithdrawTable.DateColumn + " INTEGER, " + WithdrawTable.AmountColumn + " INTEGER, " + WithdrawTable.TypeColumn  + " INTEGER, " + WithdrawTable.NotesColumn + " STRING);");
        db.execSQL("CREATE TABLE " + OptionsTable.TableName + " (" + OptionsTable.IdColumn + " INTEGER PRIMARY KEY AUTOINCREMENT, " + OptionsTable.OptionNameColumn + " STRING, " + OptionsTable.ValueColumn + " STRING);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public Cash getMonthlyBudget() {
        Cash cash = getMonthlyBudgetFromDatabase();
        return cash != null ? cash : Cash.fromPounds(0);
    }

    private Cash getMonthlyBudgetFromDatabase() {

        SQLiteDatabase db = getReadableDatabase();
        String lookupBudgetSql = "SELECT " + OptionsTable.ValueColumn + " FROM " + OptionsTable.TableName + " WHERE " + OptionsTable.OptionNameColumn + " = ?";

        Cursor cursor = db.rawQuery(lookupBudgetSql, new String[] {OptionKeyNames.MonthlyBudget});
        try {
            boolean hasItem = cursor.moveToFirst();
            if(hasItem) {
                int monthlyBudgetPence = Integer.parseInt(cursor.getString(0));
                return Cash.fromPence(monthlyBudgetPence);
            }
            else {
                return null;
            }
        }
        finally {
            cursor.close();
        }
    }

    public void setMonthlyBudget(Cash cash) {

        ContentValues values = new ContentValues();
        values.put(OptionsTable.OptionNameColumn, OptionKeyNames.MonthlyBudget);
        values.put(OptionsTable.ValueColumn, String.valueOf(cash.getPence()));

        SQLiteDatabase db = getWritableDatabase();

        if(getMonthlyBudgetFromDatabase() == null) {
            db.insert(OptionsTable.TableName, OptionsTable.OptionNameColumn, values);
        }
        else {
            String whereClause = OptionsTable.OptionNameColumn + " = ?";
            String[] whereArgs = {OptionKeyNames.MonthlyBudget};
            db.update(OptionsTable.TableName, values, whereClause, whereArgs);
        }
    }

    public ArrayList<CashWithdraw> getWithdrawsStartingThisMonth() {

        // TODO: Where data is after the start of this month
        String withdrawsSql = String.format("SELECT %s, %s, %s, %s FROM %s", WithdrawTable.AmountColumn, WithdrawTable.DateColumn, WithdrawTable.TypeColumn, WithdrawTable.NotesColumn, WithdrawTable.TableName);

        ArrayList<CashWithdraw> withdrawals = new ArrayList<CashWithdraw>();

        SQLiteDatabase db = getReadableDatabase();

        Cursor cursor = db.rawQuery(withdrawsSql, null);
        try {
            for (boolean hasItem = cursor.moveToFirst(); hasItem; hasItem = cursor.moveToNext()) {

                Cash cash = Cash.fromPence(cursor.getInt(0));
                Date date = new Date(cursor.getLong(1));
                WithdrawType type = WithdrawType.fromId(cursor.getInt(2));
                String notes = cursor.getString(3);

                withdrawals.add(new CashWithdraw(date,cash, type, notes));
            }
        } finally {
            cursor.close();
        }

        return withdrawals;
    }

    public void addNewWithdrawal(CashWithdraw withdraw) {
        ContentValues values = new ContentValues();
        values.put(WithdrawTable.AmountColumn, withdraw.getAmount().getPence());
        values.put(WithdrawTable.TypeColumn, withdraw.getWithdrawType().getId());
        values.put(WithdrawTable.DateColumn, withdraw.getDate().getTime());
        values.put(WithdrawTable.NotesColumn, withdraw.getNotes());

        SQLiteDatabase db = getWritableDatabase();
        db.insert(WithdrawTable.TableName, WithdrawTable.IdColumn, values);
    }

    public void removeWithdrawal(CashWithdraw withdraw) {
        SQLiteDatabase db = getWritableDatabase();

        String where = WithdrawTable.AmountColumn + " = ? AND " + WithdrawTable.DateColumn + " = ? AND " + WithdrawTable.TypeColumn + " = ?";
        String[] whereArgs = new String[]{
            String.valueOf(withdraw.getAmount().getPence()),
            String.valueOf(withdraw.getDate().getTime()),
            String.valueOf(withdraw.getWithdrawType().getId()),
        };

        db.delete(WithdrawTable.TableName,where, whereArgs);
    }

    public void updateWithdrawal(CashWithdraw oldWithdraw, CashWithdraw newWithdraw) {

        String where = WithdrawTable.AmountColumn + " = ? AND " + WithdrawTable.DateColumn + " = ? AND " + WithdrawTable.TypeColumn + " = ?";
        String[] whereArgs = new String[]{
                String.valueOf(oldWithdraw.getAmount().getPence()),
                String.valueOf(oldWithdraw.getDate().getTime()),
                String.valueOf(oldWithdraw.getWithdrawType().getId()),
        };

        ContentValues values = new ContentValues();
        values.put(WithdrawTable.AmountColumn, newWithdraw.getAmount().getPence());
        values.put(WithdrawTable.DateColumn, newWithdraw.getDate().getTime());
        values.put(WithdrawTable.TypeColumn, newWithdraw.getWithdrawType().getId());
        values.put(WithdrawTable.NotesColumn, newWithdraw.getNotes());

        SQLiteDatabase db = getWritableDatabase();
        db.update(WithdrawTable.TableName, values, where, whereArgs);
    }

    public void clearAllWithdrawals() {
        SQLiteDatabase db = getWritableDatabase();
        db.delete(WithdrawTable.TableName, null, null);
    }
}




Java Source Code List

uk.co.wilka.disposableincome.CalendarAccess.java
uk.co.wilka.disposableincome.CalendarProvider.java
uk.co.wilka.disposableincome.CashWithdraw.java
uk.co.wilka.disposableincome.CashWithdrawalDetailsActivity.java
uk.co.wilka.disposableincome.Cash.java
uk.co.wilka.disposableincome.DatabaseHelper.java
uk.co.wilka.disposableincome.IAccessData.java
uk.co.wilka.disposableincome.IProvideCalendar.java
uk.co.wilka.disposableincome.MainActivity.java
uk.co.wilka.disposableincome.PlusOneMonthCalendarProvider.java
uk.co.wilka.disposableincome.WithdrawDateComparer.java
uk.co.wilka.disposableincome.WithdrawType.java
uk.co.wilka.disposableincome.WithdrawalForMonthCalculator.java
uk.co.wilka.disposableincome.test.CashTest.java
uk.co.wilka.disposableincome.test.CashWithdrawTest.java
uk.co.wilka.disposableincome.test.FakeCalendarProvider.java
uk.co.wilka.disposableincome.test.PlusOneMonthCalendarProviderTests.java
uk.co.wilka.disposableincome.test.WithdrawDateComparerTests.java
uk.co.wilka.disposableincome.test.WithdrawalForMonthCalculatorTests.java