在我的应用中,我试图显示当月余额,但我无法做到这一点。
这是我的代码
// 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;
}
Run Code Online (Sandbox Code Playgroud)
当我执行该代码时,出现类似
E/SQLiteLog: (1) no such function: CURDATE
Shutting down VM
FATAL EXCEPTION: main
Run Code Online (Sandbox Code Playgroud)
根据此链接
使用date('now')代替curdate()。
为了获取当前日期,在MySQL和SQL Server中可以使用,CURDATE()但在SQLite中则没有此选项。要获取当前日期,您必须date('now')在查询中使用以获取该特定设备中的当前日期。
因此,您的查询将如下所示:
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'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3055 次 |
| 最近记录: |