0

I am new to SQLite and Android. And wanted to calculate SUM of column1 when dates in column2 is selected.I am writing following function while receiving parameters fromDate and toDate in following function:

    int tot=0;
    SQLiteDatabase db=this.getWritableDatabase();
    String query= "SELECT SUM("+ COL_3+ ")" +" AS "+"Total"+" FROM "+TABLE_NAME+ " WHERE "+ COL_4+
             " BETWEEN "+ fromDate + " AND "+toDate+" ";
    Cursor cursor=db.rawQuery(query,null);
    if(cursor.moveToFirst()){
        tot=cursor.getInt(0);
    }
    return tot;

But this is giving tot value zero. What am I doing wrong here. Any help would be much appreciated.

Thanks in advance.

2
  • Show your declaration for COL_3, TABLE_NAME, fromDate, ... Commented Apr 2, 2018 at 16:08
  • public static final String COL_3="LIT"; public static final String TABLE_NAME="table1"; fromDate is String i am getting in function Commented Apr 5, 2018 at 13:57

1 Answer 1

0

SQLLite requires dates to be in YYYY-MM-DD format. Since the data in your database and the string in your query isn't in that format, it is probably treating your "dates" as strings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.