How to return a DataPoint from a Method , I need a help on that issue

Khan Consultoria
Khan Consultoria used Ask the Experts™
on
Hello Guys

I am working with DataPoint variable and my problem is being when I need to create a method ( function ) that can result a dataPoint.

I created a method this way:

public DataPoint[] GeraGraficoEstudoMes(String Situacao, String idTema){

        int i=0;

        String wSql = " SELECT strftime(''%d'',data) AS DIA,  Count(1) AS TOTAL_ACERTO  FROM CRTGRAFICOESTUDO " +
                      " WHERE ACERTOU='" + Situacao + "'" +
                      "   AND strftime(''%Y'',data)=strftime(''%Y'',''now'') " +
                      "   AND strftime(''%m'',data)=strftime(''%m'',''now'') ";


        if (!idTema.isEmpty()) {
            wSql = wSql + " AND TAB_TEMA_ID=" + idTema;
        }

        wSql = wSql + " GROUP BY strftime(''%d'',data) ";
        wSql = wSql + " ORDER BY strftime(''%d'',data) ";


        openDataBase();
        Cursor cs = mSQSqLiteDatabase.rawQuery(wSql, null);

        DataPoint[] points = new DataPoint[cs.getCount()];

        if (cs.getCount() > 0) {

            if (cs.moveToFirst()) {
                while (!cs.isAfterLast()) {
                    points[i] = new DataPoint(cs.getInt(0), cs.getInt(1));
                    cs.moveToNext();
                }
            }
        }

        cs.close();
        closeDataBase();

        return points;
    }

Open in new window


something is wrong when I try to get the result, that way:

DataPoint[] points1 = DB.GeraGraficoEstudoMes("A", "");

I am not sure how to work around with that, I am newbier in Java and Android studio

I appreciate a lot your help with some simple example

Regards
Alex
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer
[..] something is wrong [..]
Well, any error message or exception? Stack trace?

p.s. your SQL string building allows a SQL injection attack.

Author

Commented:
Hello ste5an,

The error happens when I return my datapoints.

I'd like a simple example how to return a datapoints from a method, so that I could follow it and understand how to do.

about the SQL, what should I do to avoid it?

Thanks a lot for your help
Senior Developer
Can you imagine, that I don't have a clue what you're talking about as long as you only call it the error instead of telling us what the error actually is?

btw, you don't increment I in the loops body. Does data contain the correct values for strftime()?

public DataPoint[] GeraGraficoEstudoMes(String Situacao, Integer idTema) {

    String wSql =
        "SELECT strftime(''%d'', data) AS DIA, Count(1) AS TOTAL_ACERTO " +
        "FROM CRTGRAFICOESTUDO " +
        "WHERE ACERTOU = '{Situacao}' " +
        "  AND strftime(''%Y'', data) = strftime(''%Y'', ''now'') " +
        "  AND strftime(''%m'', data) = strftime(''%m'', ''now'') " +
        " {idTema} " +
        "GROUP BY strftime(''%d'',data) " +
        "ORDER BY strftime(''%d'',data) ";

    wSql = wSql.replace("{Situacao}", Situacao.replace("'", "''"));
    if (idTema != null) {
        wSql = wSql.replace("{idTema}", "AND TAB_TEMA_ID = " + idTema);
    }

    openDataBase();
    Cursor cs = mSQSqLiteDatabase.rawQuery(wSql, null);
    DataPoint[] points = GeraGraficoEstudoMes(cs)
    cs.close();
    closeDataBase();
    return points;
}

private DataPoint[] GeraGraficoEstudoMes(Cursor dataPoints) {
    int i = 0;
    DataPoint[] points = null;
    if (cs.getCount() > 0) {
        points = new DataPoint[cs.getCount()];
        while (!cs.isAfterLast()) {
            points[i++] = new DataPoint(cs.getInt(0), cs.getInt(1));
            cs.moveToNext();
        }
    }

    return points;
}

Open in new window

Author

Commented:
ste5an

I understood my mistake with your example and it worked. Thanks a lot for your help.

About the Query, I also got it, I didn't know about that, once more, thanks a lot.

Have a good day
Alex

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial