shz1998
shz1998

Reputation: 53

Retrieving data from multiple specific columns from a table SQLite

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";

Upvotes: 0

Views: 69

Answers (1)

Mohammad C
Mohammad C

Reputation: 1341

You did not overwrite the cursor from when you got the amount for food n drinks. So you are still working with the budgets cursor. So save the result from getLatestAmount to latestamount.

change

myDb.getLatestAmount(id, temp_category);

to

latestamount = myDb.getLatestAmount(id, temp_category);

So your code should look like this

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";
// the line below is what you needed to change
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_foodndrinks = latestamount.getDouble(0);
}

Upvotes: 1

Related Questions