Click here to Skip to main content
14,930,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I tried to make a report based on date and displays the result in the text "edt1". But no result. I think the problem is in the sql query and exactly in the specification of the "strdatedebut" and "strdatefin".

What I have tried:

there is mydatabasehelper :

public String calculrapport(Argent a)
   {
       db = this.getWritableDatabase();
           String rv = "0";
           db = this.getWritableDatabase();
           String query = "select sum(Entree) from Argent where date between \"strdatedebut\" and \"strdatefin\" ;";
           Cursor cursor = db.rawQuery(query , null) ;
           if (cursor.moveToFirst()) {
               rv = cursor.getString(0);
           }
           cursor.close();
           return rv;
       }


There is my class Rapport.java :



package com.example.pc.myapplication;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
 
public class rapport extends Activity {
 
    DatabaseHelper helper = new DatabaseHelper(this);
 
 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rapport);
 
 
    }
    public void onOKClick ( View v ) {
 
        if (v.getId() == R.id.okrapport) {
 
            EditText datedebut = (EditText) findViewById(R.id.datedebut);
            EditText datefin = (EditText) findViewById(R.id.datefin);
            TextView edt1 = (TextView) findViewById(R.id.resultatrapport);
 
            String strdatedebut = datedebut.getText().toString();
            String strdatefin = datefin.getText().toString();
 
            Argent a = new Argent();
           String sum =  helper.calculrapport(a);
          edt1.setText(sum);
 
       }
 
    }
}
Posted
Updated 7-Jul-18 6:45am
Comments
Graeme_Grant 6-Jul-18 20:18pm
   
Have you tried debugging your app? Check this link on how to: Debug your app  |  Android Developers[^]
Member 13901696 7-Jul-18 6:02am
   
Yes I tried to debug the application but i don't receive any useful information
Graeme_Grant 7-Jul-18 7:59am
   
The whole point of debugging is to check that you are getting the expected values at each line of code. If not, then look at why not. All debugging information is useful if used correctly.
Richard MacCutchan 7-Jul-18 3:26am
   
What are the values in your two date parameters, and did the SELECT return anything?
Member 13901696 7-Jul-18 6:00am
   
Thanks for tour response. The two date parameters is fixed in the class rapport.java and they changed always. and the select doesn't return anything.
Richard MacCutchan 7-Jul-18 6:08am
   
Then you know where to look to diagnose the problem.

1 solution

Your query doesn't look right to me.
String query = "select sum(Entree) from Argent where date between \"strdatedebut\" and \"strdatefin\" ;";

This looks like it will send the two literal strings strdatedebut and strdatefin rather than inserting variables. Try this?
String query = "select sum(Entree) from Argent where date between '" + strdatedebut + "' and '" + strdatefin + "' ;";

Note that's single quote around the insertion of the variables holding the date.
BTW one way that you can debug these things is to copy the contents of query to the clipboard and paste it into a query window. When you try to run it you might get an error in the results window.
   
Comments
Richard MacCutchan 7-Jul-18 12:55pm
   
Of course, well spotted.
Member 13901696 8-Jul-18 6:47am
   
Thanks to all of you it'works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900