Calculate sql table in android

Asked

Viewed 111 times

1

I have the following sqlite database and works well:

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

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

Right now I have working the search between dates, I intend the same, but now adding up 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 show on the screen by pressing a button for example. In other words, by pressing the button you add all the fields of the total table. Valor1+valor2+valor.

  • Well what I want is to return a sum and show on the screen by pressing a button for example. In other words, pressing the button adds all the fields of the total table. Value1+value2+value

  • Good here is the code: http://35.167.140.166/site/codigo.txt I want that when pressing the button see, it is possible to obtain the total of the table "total" that is to add all the values that are inserted and show a screen message with the sum.

1 answer

0

Use

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

As SQL model. Next, how would the code look:

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);
  • Query its nice now, and how show? :/

  • The query does not give error, now I can not show, as I show? !!

  • Thank you very much! Now says that the "grandtotal" has not been initialized that I should do?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.