mardi 15 décembre 2015

Getting sum of a value in SQLite table between two date values in android

I am trying to get the sum of a column between two dates.But instead of the sum between two dates I am getting the whole sum.The following is what i have tried.

The code for creating the sqlite table:

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

    }
}

I am executing the query from activity as follows:

DataBase db=new DataBase(this);
int sum;
sum=db.calculateSumDebit(start_date, end_date);

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 amount columns instead of the sum between the required dates.Please help.

Aucun commentaire:

Enregistrer un commentaire