Calculate sql table in android

1

I have the following sqlite database and it works fine:

db=openOrCreateDatabase("BaseDadosDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS tabeladados(data date,total real,descricao VARCHAR);");

I want to return the sum of the total table between dates.

At this point I have to search between dates, I intend the same, but now add the total.

Code:

if(view==btnVer) {
    if (data.getText().toString().trim().length() == 0) {
        alert("Erro data inicial", "Tem que inserir uma data para iniciar a pesquisa");
        return;
    }
    if (dataa.getText().toString().trim().length() == 0) {
        alert("Erro data final", "Tem que inserir uma data para iniciar a pesquisa");
        return;
    }


    Cursor c = db.rawQuery("SELECT * from tabeladados WHERE data BETWEEN '"+data.getText()+"' AND '"+ dataa.getText()+"' ORDER BY data DESC", null);
    if(c.getCount()==0) {
        alert("Erro!", "Sem resultados entre as datas " + data.getText()+" E "+dataa.getText());
        return;
    }

    StringBuffer buffer=new StringBuffer();
    while(c.moveToNext())
    {

        buffer.append("Data:"+c.getString(0)+"\n");
        buffer.append("Total Horas: "+c.getString(1)+"\n");
        buffer.append("Descricão: "+c.getString(2)+"\n\n");
    }
    alert("Registos da data "+data.getText()+" A " + dataa.getText(), buffer.toString());    

}

What I want is to return a sum and display on the screen at the press of a button for example. That is, by pressing the sum button all fields in the total table. Valor1+valor2+valor .

    
asked by anonymous 12.01.2017 / 16:36

1 answer

0

Use

"SELECT sum(total) as GrandTotal from tabeladados WHERE data BETWEEN '"+data.getText()+"' AND '"+ dataa.getText()+"' ORDER BY data DESC"

As the SQL model. Here's what the code would look like:

Double grandTotal;
Cursor c = db.rawQuery("SELECT * from tabeladados WHERE data BETWEEN '"+data.getText()+"' AND '"+ dataa.getText()+"' ORDER BY data DESC", null);
    if(c.getCount()==0) {
        alert("Erro!", "Sem resultados entre as datas " + data.getText()+" E "+dataa.getText());
        return;
    }

    StringBuffer buffer=new StringBuffer();
    while(c.moveToNext())
    {

        buffer.append("Data:"+c.getString(0)+"\n");
        buffer.append("Total Horas: "+c.getString(1)+"\n");
        buffer.append("Descricão: "+c.getString(2)+"\n\n");
    }
    alert("Registos da data "+data.getText()+" A " + dataa.getText(), buffer.toString());
Cursor cur = db.rawQuery("SELECT sum(total) as GrandTotal from tabeladados WHERE data BETWEEN '"+data.getText()+"' AND '"+ dataa.getText()+"' ORDER BY data DESC", null); 
if(cur.moveToFirst()) { 
    grandTotal = cur.getDouble(0); 
}
alert("Total: "+ String.valueOf(grandTotal);
    
12.01.2017 / 16:57