我试图得到两列的总和并从第一列中减去第二列(两者都是正值。)
public int getSum(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT SUM (" + KEY_CREDIT + ") - SUM (" + KEY_DEBIT + ") AS myTotal FROM " + TABLE_DEFAULT_ACCOUNT, null);
if(c.moveToFirst()) {
sum = c.getInt(c.getColumnIndex("myTotal"));
}
return sum;
}
由于某种原因它返回0,即使在Credit列中的数据肯定是。现在它应该是27 - 0 = 27
如果我将其作为
运行// GET SUM
public int getSum(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT SUM (" + KEY_CREDIT + ") FROM " + TABLE_DEFAULT_ACCOUNT, null);
if(c.moveToFirst()) {
sum = c.getInt(0);
}
return sum;
}
它返回27就好了。
感谢任何帮助!
答案 0 :(得分:0)
以下对我有用:
// GET SUM
public int getSum(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT SUM(credit), SUM(debit) FROM " + TABLE_DEFAULT_ACCOUNT, null);
if(c.moveToFirst()) {
sum = (c.getInt(0) - c.getInt(1));
}
return sum;
}