当前位置:首页 > 数据库 > SQlite

使用SQLite从android中选择当前月份的数据

参见英文答案 > MySQL Date and Time functions don’t exist                                    3个
在我的应用程序中,我试图显示当月的余额,但我无法做到这一点.

这是我的代码

// Count Current Month Income
public float CurrentMonthIncome(){
    float x = 0;
    SQLiteDatabase db = getReadableDatabase();
    String getamountdata = "SELECT SUM(inc_amount) AS totalInc FROM "+ TABLE_ENTRY + " where year(entry_date)= year(CURDATE()) and MONTH(entry_date)=MONTH(CURDATE())";
    Cursor c = db.rawQuery(getamountdata, null);
    if(c.moveToFirst()){
        x = c.getFloat(0);
    }
    return x;

}

当我执行该代码时,我得到一个例外

E/SQLiteLog: (1) no such function: CURDATE
Shutting down VM
FATAL EXCEPTION: main

解决方法:

根据这个link
使用date(‘now’)而不是curdate().

为了获取当前日期,在MySQL和SQL Server中,您可以使用CURDATE(),但在SQLite中,您没有此选项.要获取当前日期,您必须在查询中使用日期(“现在”)来获取该特定设备中的当前日期.

所以你的查询将是这样的:

SELECT SUM(inc_amount) AS totalInc 
FROM "+ TABLE_ENTRY + " WHERE strftime('%Y',entry_date) = strftime('%Y',date('now')) AND  strftime('%m',entry_date) = strftime('%m',date('now'))

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!