我正在开发一个管理用户费用和收入的应用程序,我已经到了一个非常困难的地步,觉得我需要一些帮助,我想做的是保存总用户余额的记录并能够对其进行计算,根据用户的操作,如果他的余额是 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;
}
}