我有一个查询
SELECT SUM(amount) AS total FROM transactions WHERE transaction_date > 1477333800
对于此查询,cursor.getCount()
返回 1
。但是当遍历游标时,该值变为空。
public Cursor getTotalForToday(long start)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT SUM(amount) AS total FROM "+TRANSACTION_TABLE+" WHERE transaction_date > "+start, null);
return c;
}
Cursor cursor = db.getTotalForToday();
if(cursor.getCount() == 0) // this is evaluating to false
{
forToday.setText("Zero"); //forToday is TextView
}
else
{
while(cursor.moveToNext())
{
String total_today = cursor.getString(0); // this is returning null
forToday.setText(total_today);
}
}
PS:表中amount
的数据类型为整数
最佳答案
聚合 SQL 函数(例如 SUM()
)始终返回结果行。结果本身可以为空。
关于java - cursor.getCount() 返回 1 但应该返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40234304/