简体   繁体   English

在android中的两个日期值之间获取SQLite表中的值的总和

[英]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: 用于创建sqlite表的代码:

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: 我正在从activity执行查询,如下所示:

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

The code for the query in DataBase class: DataBase类中的查询代码:

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. 我得到了整个金额列的总和,而不是所需日期之间的总和。请帮忙。

you have used ' current_date ' as column name which is a keyword in sqlite. 您使用' current_date '作为列名,这是sqlite中的关键字。 This will return current date always. 这将始终返回当前日期。 please change column name like ' curr_date ' . 请更改列名称' curr_date '。 Also there is no date datatype in sqlite make it Text and use query like this 在sqlite中也没有日期数据类型使它成为文本并使用这样的查询

SELECT SUM(amount) FROM demo WHERE date(curr_date) BETWEEN date("2015-12-16") AND date("2015-12-17") SELECT SUM(金额)FROM演示WHERE日期(curr_date)BETWEEN日期(“2015-12-16”)和日期(“2015-12-17”)

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM