用最简单的术语来说,如果我有一个表格,其值如下
x 5
x 7
y 3
v 19
x 24
我希望将x的所有int值相加,不包括y和v等
我目前有工作代码返回x的所有列值(在本例中为dateAccessed)
public ArrayList<HashMap<String, String>> searchByDate() {
String dateAccessed = FoodDiary.dateAccessed;
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
SQLiteDatabase database = this.getReadableDatabase();
//String selectQuery = "SELECT * FROM diarys";
String selectQuery = "diarys";
Cursor cursor = database.query(selectQuery, new String[] { "diaryId",
"diaryName", "diaryDate", "diaryNumber" }, "diaryDate LIKE '"
+ dateAccessed + "%'", null, null, null, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("diaryId", cursor.getString(0));
map.put("diaryName", cursor.getString(1));
map.put("diaryDate", cursor.getString(2));
map.put("diaryNumber", cursor.getString(3));
wordList.add(map);
} while (cursor.moveToNext());
}
return wordList;
}
我还有用于返回整列的SUM的工作代码
int total = 0;
Cursor cursor = database.rawQuery("SELECT SUM(builderNumber) FROM builders", null);
if (cursor.moveToFirst())
{
total = cursor.getInt(0);
} while (cursor.moveToNext());
return total;
然而,我似乎无法将两者结合起来让它们一起工作。
无论我构建光标的方式如何,它总是会失败。
非常感谢任何帮助
答案 0 :(得分:1)
如果您在开头的
中对您的简单示例进行原始查询select sum(dairyNumber) from yourtable where dairyId = 'x'
or, if you want another column than just the sum .
select diaryName, sum(dairyNumber) from yourtable where dairyId = 'x'
如果你想传递'x'的值作为参数,你可以使用?并提供一个where参数。
对于您的具体示例,您的原始查询应该类似于
select sum(diaryNumber) from yourtable where diaryDate like '201307%'
select diaryName, sum(diaryNumber) from yourtable where diaryDate like '201307%'
希望这有帮助。