Bringing a sum in SQLite Android

1

I have a problem in developing an App, following, I have a list of all donation data in the database, and in this activity I created a TextView to receive the full value of those donations, however, I have some problems: 1st the value field in the bank saved in TEXT exp: 'R $ 120,00', 'R $ 325,65' and so I make a sum with substr; 2 ° when I try to bring the result of that sum actually in the app it leaves the entire query text, I tried in several ways and this is the current situation of my code:

    public String RetornarTotal(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor stmt = db.rawQuery("SELECT SUM(SUBSTR(VALOR,3,20)) FROM " + TABLE_NAME, null);

    String total = stmt.getColumnName(0).toString();
    //String total = stmt.execute();
    return total;
}

This method is called in the oncrete of activity and gets in the SQLHelper class, I want to return only the value of that sum, could you help me?

    
asked by anonymous 23.08.2017 / 03:23

2 answers

0

Use the getColumnIndex method to retrieve the first value of your query. Here's how it should be done:

Cursor stmt = db.rawQuery("SELECT SUM(SUBSTR(VALOR,3,20)) FROM " + TABLE_NAME, null); 

if (stmt.getCount() > 0) { 
    stmt.moveToFirst(); 
    do { 
           total = stmt.getString( stmt.getColumnIndex(0));
     } while (stmt.moveToNext()); 
         stmt.close(); 
     }
}
    
23.08.2017 / 04:35
0

Just fix this line:

String total = stmt.getColumnName(0).toString();

By:

String total = stmt.getString(0);
stmt.close();
    
23.08.2017 / 05:57