title: Android database working with currency/balance
body: <p>I am developing an app that manages users expenses and incomes, I have reached a point where I am pretty stuck and feel like I need some help, What I want to do is save have a record of the total user balance and to be able to do calculations on it, depending on the users actions, If hes balance is 100 dollars and he adds an expense of 50 dollars, the saved balance will be updated to total of 50 dollars etc..</p>
  
  <p>I am not sure what is the right way to do it or how to write the database function to do the math on the "balance" variable, this is my database class so far, I would really appreciate any example or tip.</p>
  
  <p>Right now all I am having is the basic "add,update,remove,fetch" functions...</p>
  
  <pre><code>public class TransactionsDatabase {
  
  private static final String DATABASE_NAME = "transactions_db";
  private static final String DATABASE_TABLE = "transactions_table";
  private static final int DATABASE_VERSION = 1;
  
  private static final String TRANSACTION_ID = "_id";
  private static final String TRANSACTION_AMOUNT = "amount";
  private static final String TRANSACTION_DESCRIPTION = "description";
  private static final String TRANSACTION_DATE = "date";
  private static final String TRANSACTION_CATEGORY = "category";
  private static final String TRANSACTION_CURRENCY = "currency_type";
  private static final String TRANSACTION_EXPENSE_OR_INCOME = "expenseOrIncome";
  private static int BALANCE; // this is how I have tried to use the balance so far...
  
  private static final String CREATE_DATABASE = "create table transactions_table (_id integer primary key autoincrement, "
  + "amount integer not null, date text not null, category text not null, currency_type text not null, description text not null, expenseOrIncome text not null);";
  
  private static final String TAG = "TransactionsDatabase";
  private DatabaseHelper DbHelper;
  private SQLiteDatabase SqlDatabase;
  private final Context ctx;
  
  private static class DatabaseHelper extends SQLiteOpenHelper {
  
      public DatabaseHelper(Context context) {
  
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
  
          db.execSQL(CREATE_DATABASE);
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  
          Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                  + newVersion + ", which will erase old data");
          db.execSQL("DROP TABLE IF EXISTS expenses_table");
          onCreate(db);
      }       
  }
  
  TransactionsDatabase (Context c) {
      this.ctx = c;
  }
  
  public TransactionsDatabase open() throws SQLException {
  
      DbHelper = new DatabaseHelper(ctx);
      SqlDatabase = DbHelper.getWritableDatabase();
      return this;
  
  }
  
  public void close() {
  
      DbHelper.close();
  }
  
  public long createExpenseOrIncome (int amount, String description, String date, String category, String currency_type, String expenseOrIncome) {
  
      ContentValues values = new ContentValues();
      values.put(TRANSACTION_AMOUNT, amount);
      values.put(TRANSACTION_DESCRIPTION, description);
      values.put(TRANSACTION_DATE, date);
      values.put(TRANSACTION_CATEGORY, category);
      values.put(TRANSACTION_CURRENCY, currency_type);
      values.put(TRANSACTION_EXPENSE_OR_INCOME, expenseOrIncome);
  
      // Returns the row ID of newly inserted row, or -1 if an error occurred.
      return SqlDatabase.insert(DATABASE_TABLE, null, values);
  }
  
  public boolean deleteExpenseOrIncome(long rowId) {
  
      // Returns true if deleted, false otherwise.
      return SqlDatabase.delete(DATABASE_TABLE, TRANSACTION_ID + "=" + rowId, null) &gt; 0;
  
  }
  
  public Cursor fetchAllExpensesAndIncomes() {
  
      // Returns a cursor over the list of all expenses/incomes.
  
      return SqlDatabase.query(DATABASE_TABLE, new String[] { TRANSACTION_ID,
              TRANSACTION_AMOUNT, TRANSACTION_DESCRIPTION, TRANSACTION_DATE, TRANSACTION_CATEGORY, TRANSACTION_CURRENCY, TRANSACTION_EXPENSE_OR_INCOME }, null, null, null, null,
              null);
  }
  
  public Cursor fetchSpecificExpenseOrIncome(long rowId) throws SQLException  {
  
          Cursor mCursor = SqlDatabase.query(true, DATABASE_TABLE, new String[] { 
                  TRANSACTION_AMOUNT, TRANSACTION_DESCRIPTION, 
                  TRANSACTION_DATE, TRANSACTION_CATEGORY, TRANSACTION_CURRENCY, TRANSACTION_EXPENSE_OR_INCOME },
                  TRANSACTION_ID + "=" + rowId, null, null, null, null, null);
  
          if (mCursor != null) {
              mCursor.moveToFirst();
          }
              return mCursor;
      }
  
  public boolean updateExpenseOrIncome(long rowId, int amount, String description, String date, String category, String currency_type, String expenseOrIncome) {
  
      // returns true if the expense/income was successfully updated, false otherwise.
  
      ContentValues values = new ContentValues();
      values.put(TRANSACTION_AMOUNT, amount);
      values.put(TRANSACTION_DESCRIPTION, description);
      values.put(TRANSACTION_DATE, date);
      values.put(TRANSACTION_CATEGORY, category);
      values.put(TRANSACTION_CURRENCY, currency_type);
      values.put(TRANSACTION_EXPENSE_OR_INCOME, expenseOrIncome);
  
      return SqlDatabase.update(DATABASE_TABLE, values, TRANSACTION_ID + "=" + rowId,  null) &gt; 0;
  
  }
  
  public boolean deleteAllExpensesOrIncomes() {
  
      // true if any number of rows were deleted, false otherwise.
  
      return SqlDatabase.delete(DATABASE_TABLE, null, null) &gt; 0;
  }
  
  public boolean deleteSpecificExpenseOrIncome(long rowId) {
  
      // true if a row is deleted, false otherwise.
  
      return SqlDatabase.delete(DATABASE_TABLE, TRANSACTION_ID + "=" + rowId, null) &gt; 0;
  }
  </code></pre>
  
  <p>}</p>