java - cursor.getCount() 返回 1 但应该返回 0

标签 java android

我有一个查询

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的数据类型为整数

如果我在 SQLLite Firefox 扩展中查询相同的内容,我将得到以下输出 SS1

理想情况下,返回的行数应该为 0,但我不知道为什么它返回为 1。 顺便说一句,这是表中的数据: SS2

最佳答案

聚合 SQL 函数(例如 SUM())始终返回结果行。结果本身可以为空。

关于java - cursor.getCount() 返回 1 但应该返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40234304/

相关文章:

java - Android - 如何将五个变量值传递给后台线程并从中接收五个值?

android - 使用接口(interface)在 fragment 和 fragment Activity 之间进行通信是否好

android - 获取Android中videoView的mediaPlayer

java - 无法解析 JSON 对象数组

java - 如何在 C# 或 Java 中找到每个 Switch Case 项的值?

java - 打开位于资源文件夹中的 pdf 文件

java - 使用 PowerMockito 模拟 getClass 方法

java - 使用 AsyncTask 在 ListView 中显示图像

android - 当 isDetached() 返回 false 时,getActivity() 返回 null

android - 如何在 Kotlin 中设置可见性?