我正在開發一個管理用戶開支和收入的應用程序,我已經達到了一個點,我很困難,覺得我需要一些幫助,我想要做的是保存有記錄的總用戶餘額,並且能夠根據用戶的操作對其進行計算,如果他的餘額是100美元並且他增加了50美元的開支,則所存儲的餘額將被更新爲總共50美元等。Android數據庫使用貨幣/餘額
我不知道什麼是正確的方法來做到這一點或如何編寫數據庫函數來做「平衡」變量的數學,這是我的數據庫類迄今,我真的很感激任何示例或提示。
現在所有我有是基本的「添加,更新,刪除,獲取」功能...
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;
}
}
謝謝你的迴應,我很高興有人可以指出我正確的方向,現在關於費用或收入,我使用了一個無線電組2選擇,以確定哪一個是費用或收入條目,我會如果你能幫助我實現你的建議,我們建議使用1個柱子。 –
從輸入片段/活動中保存數據時,請執行以下操作:'boolean isExpense =/* *代碼以確定它是費用還是收入* /; int amount =/*獲取交易金額的代碼* /; if(isExpense){amount * = -1; }這翻轉了交易金額的標誌,如果這是一筆開支,它將與輸入的內容相反。假設你的用戶輸入了有效的數據,這總是會對收入產生積極影響,對消費產生負面影響。然後,您只需將此值存儲在TRANSACTION_AMOUNT列中。 –
完美!這種方式,我甚至不需要費用或收入列!哦,但如果我放棄這個專欄我將如何跟蹤歷史記錄?像用戶之前插入的所有費用和收入?我的意思是必須有一種方法來保存每筆交易,並跟蹤每筆交易的情況。 –