问题描述
我是新来的SQLite和Java,和我想要学习上飞的东西。我有一个具有一定数值在其列,我想获得它的款项,并在TextView中显示出来。
I'm new to SQLite and Java, and I'm trying to learn things on the fly. I have a column that has some numeric values in it, and I would like to get the sum of it and display it in a textview.
我目前的code是这样的:
My current code is this:
public Cursor getTotal() {
return sqliteDatabase2.rawQuery(
"SELECT SUM(COL_VALUES) as sum FROM myTable", null);
}
我不知道如果code是正确的,但。
I'm not sure if that code is correct, though.
我知道我应该去取那code中的结果,但我不能确定如何做到这一点。我怎样才能得到这个查询的结果到我的Java code?
I know that I'm supposed to fetch the results of that code, but I'm unsure how to do it. How can I get the results of this query into my Java code?
推荐答案
之和将返回结果与一行和一列,因此您可以使用游标读取该值:
The sum will be returned as a result with one row and one column so you can use the cursor to fetch that value:
Cursor cursor = sqliteDatabase2.rawQuery(
"SELECT SUM(COL_VALUES) FROM myTable", null);
if(cursor.moveToFirst()) {
return cursor.getInt(0);
}
这篇关于在Android上获取一个SQLite SUM在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!