我已经环顾四周,发现了一些类似的案例,但没有一个案例需要在条目中加以特异性。我在这里。
我有一个方法filterPayments
,它根据特定的PayTable
返回GroupID
中的所有条目,然后显示在我的GridView
中。从那里,我想总结PayTable
中5列中的2列的值,特别是我的Interest
和Due
列。我不确定如何在查询中执行此操作,更不用说仅针对特定列执行此操作。
问题
如何添加特定列中所有条目的值。
是否可以在SQLite查询中执行此操作? 如果,如何使用filterPayments
的返回值并仅对特定列执行求和? 如果不是那么我该怎么做?
以下是我的代码段。
filterPayments
Cursor filterPayments(String Payment) {
SQLiteDatabase db = this.getWritableDatabase();
String[] columns = new String[]{"_id", colGroupID, colPayBal, colInterest, colDue, colDateDue, colPaid};
Cursor c = db.query(viewPmnts, columns, colGroupID + "=?", new String[]{Payment}, null, null, null);
return c;
}
GridView
public void Paygrid() {
dbHelper = new DatabaseHelper(this);
String Payment = String.valueOf(txt.getText());
Cursor a = dbHelper.filterPayments(Payment);
startManagingCursor(a);
String[] from = new String[]{DatabaseHelper.colPayBal, DatabaseHelper.colInterest, DatabaseHelper.colDue, DatabaseHelper.colDateDue, DatabaseHelper.colPaid};
int[] to = new int[]{R.id.Amount, R.id.Interest, R.id.Due, R.id.DateDue, R.id.Paid};
SimpleCursorAdapter saa = new SimpleCursorAdapter(this, R.layout.paygrid, a, from, to);
intgrid.setAdapter(saa);
}
答案 0 :(得分:1)
我建议从列中提取所有数据,然后在Java或android中对它们求和。那将是最简单的方法。
没有核心的sqlite函数可以做到这一点。 https://www.sqlite.org/lang_corefunc.html
但是,您可以创建自定义sqlite函数。往下看。 How to create custom functions in SQLite
答案 1 :(得分:1)
希望您的问题正确。但是,如果您有两列名称分别为interest
和due
的列,则可以通过SQL查询
SELECT interest + due FROM PayTable;
这也适用于乘法(及其逆运算)。不幸的是,对于非整数取幂(例如平方根),它变得更加棘手。据我所知,您需要已经提到的自己的SQLite函数。如果幸运的话,您可以从C标准库中加载包装math.h
的模块(搜索extension-functions.c)
有关表中汇总的其他方式,请查看this question for PostgreSQL(对于SQLite来说是相同的)