I'm trying to retrieve data from multiple specific columns of a table in SQLite but with the code below, I'm not sure why but instead of getting data from two different columns it just gets the data from the very first column which I put in my method, which is the "budget" column of the "spendings" table :-
String temp_category = "budget";
Cursor latestamount = myDb.getLatestAmount(id, temp_category);
if (latestamount.getCount() == 0) {
Toast.makeText(AddExpenses.this, "No amount found !", Toast.LENGTH_LONG).show();
} else {
latestamount.moveToFirst();
amountfromdb_budget = latestamount.getDouble(0);
}
temp_category = "foodndrinks";
myDb.getLatestAmount(id, temp_category);
if (latestamount.getCount() == 0) {
Toast.makeText(AddExpenses.this, "No amount found !", Toast.LENGTH_LONG).show();
} else {
latestamount.moveToFirst();
amountfromdb_foodndrinks = latestamount.getDouble(0);
}
Using the code above, both my "amountfromdb_budget" and "amountfromdb_foodndrinks" will be inserted with the value in the column "budget" of my SQLite. My goal is to retrieve the value in the "budget" column into "amountfromdb_budget" and value in "foodndrinks" column into "amountfromdb_foodndrinks".
Below is my "getLatestAmount" method in my DatabaseHelper class :-
public Cursor getLatestAmount(String id, String category) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor latestamount = db.rawQuery("SELECT "+category+" FROM spendings WHERE id =" +id , null);
return latestamount;
}
And the structure of my "spendings" table is below :-
private static final String SPENDING_TABLE = "spendings";
private static final String ID_SPENDING = "id";
private static final String BUDGET = "budget";
private static final String TOTAL_EXPENSES = "total_expenses";
private static final String FOODNDRINKS = "foodndrinks";
private static final String TRAVEL = "travel";
private static final String SHOPPING = "shopping";
private static final String ENTERTAINMENT = "entertainment";
private static final String OTHERS = "others";
private static final String BALANCE = "balance";