2016-10-25 14 views
0

を返されている必要があります私はこのクエリのcursor.getCount(1)を返すが、0

SELECT SUM(amount) AS total FROM transactions WHERE transaction_date > 1477333800 

でクエリを持って、cursor.getCount()1を返しています。しかし、カーソルを反復するとき、値はnullになります。

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

答えて

2

SUM()などの集計SQL関数は、常に結果行を返します。結果自体はnullでもかまいません。

+0

ダーン!これは私が今考え出したものです。最初のスクリーンショットの赤い色の背景は良いヒントでした。 – asprin

関連する問題