我试图提出命令来计算级别“2”中字母“B”的数量时遇到一些麻烦。
_id|letter|level
0 A 1
1 A 1
2 B 1
3 A 2
4 B 2
5 A 2
6 B 2
7 B 2
8 B 2
9 B 2
这是我目前在DBAdapter中的“代码”。
public long getNumberCorrect() {
int i = 0;
String query = "SELECT **NUMBER OF LETTER B** FROM table WHERE level = 2";
Cursor cursor = mDb.rawQuery(query,null);
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() != true) {
}
}
return i;
}
理想情况下,上面的代码会返回5,但我不知道用什么来代替“字母B ”。
答案 0 :(得分:1)
您是否尝试过使用count(*)
?
Something like:
String query = "SELECT count(*) FROM table WHERE level = 2 and letter='B'";
Cursor cursor = mDb.rawQuery(query,null);
if (cursor.getCount() >0 && cursor.moveToFirst()) {
i = cursor.getInt(1);
}
return i;
答案 1 :(得分:0)
试试这个::
SELECT count(*) FROM table WHERE level = 2 and letter='B'
或者你可以
SELECT count(id) FROM table WHERE level = 2 and letter='B'
答案 2 :(得分:0)
这是一个很好的要求:
select count(*) from table where level=2 AND letter like 'B'
答案 3 :(得分:0)
写下面的查询
String query = "SELECT COUNT(*) FROM table WHERE level = 2 AND letter='B'";
答案 4 :(得分:0)
你可以这样做:
String sqlite = "SELECT count(_id) AS _lCount FROM table WHERE level = 2 and letter='B'";
Cursor cursor = mDb.rawQuery(query,null);
if(cursor != null){
if(cursor.getCount() > 0){
cursor.moveToFirst();
int count = cursor.getInt(cursor.getColumnIndex("_lCount"));
}
}
这应该可以解决问题。 :)