Dissplay result sqlite query in textview

Multi tool use
Multi tool use


Dissplay result sqlite query in textview



I tried to calculate a report and displays the result in the texview "edt1". But it's not displayed.



there is mydatabasehelper :


public void calculrapport(Argent a)
{
db = this.getWritableDatabase();

String query = "select sum(Entree) from Argent where date between "datedebut" and "datefin" ;";

Cursor cursor = db.rawQuery(query , null) ;
int count = cursor.getCount();

}



There is my class Rapport.java :


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

String strdatedebut = datedebut.getText().toString();
String strdatefin = datefin.getText().toString();

Argent a = new Argent();
helper.calculrapport(a);
edt1.setText( );

}

}



Thanks in advance.




1 Answer
1



Assuming that the query works as expected (especially considering that variables used have the appropriate scope, which would appear unlikely perhaps try using select sum(Entree) from Argent to test without the complications of wheteher or not the datedebut and datefin variables can resolve and if so resolve to usable values) then you need to :-


select sum(Entree) from Argent



Extract the appropriate value and return the value from the method and then use the returned value to set the text in the TextView.



To return the value the method should not be void, but have an appropriate return type (String will be used for the example),



so instead of public void calculrapport(Argent a), use public String calculrapport(Argent a) (thus the method will be required to return a string)


public void calculrapport(Argent a)


public String calculrapport(Argent a)



To extract the value the cursor needs to be moved to the appropriate row (there should only be the one row as the sum function is an aggregate function and there is only the one group (aggregate functions work on gropus) i.e. all rows)



As such the method could be :-


public String calculrapport(Argent a)
{
String rv = "0"; //<<<< used to return 0 if no rows are selected by the query
db = this.getWritableDatabase();
String query = "select sum(Entree) from Argent where date between "datedebut" and "datefin" ;";

Cursor cursor = db.rawQuery(query , null) ;
if (cursor.moveToFirst()) {
rv = cursor.getString(0); //<<<< get the value from the 1st (only) column
}
cursor.close(); //<<<< Cursors should always be closed when done with
return rv;
}



To set the TextView with the returned value instead of using :-


helper.calculrapport(a);
edt1.setText( );



Use :-


edt1.setText(helper.calculrapport(a));



Or :-


String sum = helper.calculrapport(a);
edt1.setText(sum);



The problem is located in the SQlite query (select sum(Entree) from
Argent where date between "datedebut" and "datefin" ;) exactly
when we call "datedebut" and "datefin" taht located in the class
rapport.java



Then String query = "select sum(Entree) from Argent where date between "datedebut" and "datefin" ;";


String query = "select sum(Entree) from Argent where date between "datedebut" and "datefin" ;";



resolves to :-


select sum(Entree) from Argent where date between "datedebut" and "datefin" ;



I believe, assuming that datedebut and datefin are string variables, and that they are in a valid SQLite date format e.g. they could be 2018-01-01 and 2018-02-01 (and that values in the date column are formatted as a valid SQLite date format) that you should instead be using :-


String query = "select sum(Entree) from Argent where date between '" + datedebut + "' and '" + datefin +"' ;";



Which would then resolve to something like :-



SELECT sum(entree) FROM argent WHERE date BETWEEN '2018-01-01' AND '2018-02-01';


SELECT sum(entree) FROM argent WHERE date BETWEEN '2018-01-01' AND '2018-02-01';



For valid SQLite date formats; refer to Date And Time Functions





thanks . I used your advice but it does not give me any result and at the same time the logcat does not displayed any error. Is there any other way please ?
– jalel
Jul 3 at 0:04





Really the correct way is for you to learn how to debug code. e.g. why not, at first just use edt1.setText("this is some text for testing" );. Does that set the text? if so then you split the problem/issue into being with the calculrapport method, otherwise there is a problem with setting the EditText. You could do far worse than look at Debug your app.
– MikeT
Jul 3 at 5:34


edt1.setText("this is some text for testing" );


calculrapport





Thanks for your advice. The problem is located in the SQlite query (select sum(Entree) from Argent where date between "datedebut" and "datefin" ;) exactly when we call "datedebut" and "datefin" taht located in the class rapport.java.
– jalel
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

u0h,Y7G MuQ5v7I Td 0faM,g
L3dT,YUq

Popular posts from this blog

Rothschild family

Cinema of Italy