0

我正在开发一个管理用户费用和收入的应用程序,我已经到了一个非常困难的地步,觉得我需要一些帮助,我想做的是保存总用户余额的记录并能够对其进行计算,根据用户的操作,如果他的余额是 100 美元,并且他增加了 50 美元的费用,则保存的余额将更新为总计 50 美元等。

我不确定什么是正确的方法,或者如何编写数据库函数来对“平衡”变量进行数学运算,到目前为止,这是我的数据库类,我非常感谢任何示例或提示。

现在我所拥有的只是基本的“添加、更新、删除、获取”功能......

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) > 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) > 0;

}

public boolean deleteAllExpensesOrIncomes() {

    // true if any number of rows were deleted, false otherwise.

    return SqlDatabase.delete(DATABASE_TABLE, null, null) > 0;
}

public boolean deleteSpecificExpenseOrIncome(long rowId) {

    // true if a row is deleted, false otherwise.

    return SqlDatabase.delete(DATABASE_TABLE, TRANSACTION_ID + "=" + rowId, null) > 0;
}

}

4

1 回答 1

0

由于这只是一个单用户 Android 应用程序,我将跳过有关缓存计算值的部分(因为 SQLite 查找不是很耗时)。但是,如果您认为需要额外的性能,则绝​​对应该考虑使用第二个表来缓存计算值,该值会在您重新运行计算时更新。

现在,关于你真正的问题:

假设您想编写一个基本函数来根据您的交易和一些基础值获取总余额。它可能看起来有点像这样:

public int getBalance(int baseValue) {
    String[] cols = new String[] {
        "SUM(" + TRANSACTION_AMOUNT + ")"
    };
    try {
        Cursor data = SqlDatabase.query(DATABASE_TABLE, cols, null, null, null, null, null);
        return data.getInt(data.getColumnIndexOrThrow(cols[0])) + baseValue;
    } catch(IllegalArgumentException e) {
        Log.e("MyApp", "Couldn't get balance", e);
        return 0;
    }
}

SUM(..)SQL 函数返回列值的总和。

当然,此代码不会立即生效,因为一方面,您有一个单独的expenseOrIncome列。有几种方法可以解决这个问题。首先:您可以将内容重写为仅使用一列,并将负值视为费用,将正值视为收入。

或者,您可以执行两个查询。第一个将选择您的收入,第二个将选择您的支出。然后你会从收入中减去费用来获得你的余额。

于 2013-07-07T15:53:19.373 回答