mardi 15 décembre 2015

How to get sum of a column between two dates in Android SQLite

I am trying to get the sum of a particular column between two date type values in Android.The dates are inserted in "YYYY-MM-DD" format.Instead of between the two dates i am getting the sum of the entire column.

The code for creating the sqlite table(I have created the current date as date type):

public class DataBaseHelper extends SQLiteOpenHelper {
    public DataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table demo(id integer primary key,current_date date,transaction_type text,category text,amount text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

The code for the query in DataBase class:

public int calculateSumDebit(String start_date, String end_date)
    {
       int sum=0;
        SQLiteDatabase sqldg=database.getReadableDatabase();

       Cursor cur = sqldg.rawQuery("SELECT sum(amount) FROM demo WHERE current_date  BETWEEN '" + start_date + "' AND '" + end_date + "'", null);
        if(cur.moveToFirst())
        {
            sum= cur.getInt(0);
        }
        return sum;
    }

I am getting the sum of the entire column instead of between the two dates required.Please Help!!

Aucun commentaire:

Enregistrer un commentaire