如何使用SQLite获取Android中查询的行数?我下面的方法似乎不起作用。
public int getFragmentCountByMixId(int mixId) {
int count = 0;
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
Cursor cursor = db.rawQuery(
"select count(*) from downloadedFragement where mixId=?",
new String[]{String.valueOf(mixId)});
while(cursor.moveToFirst()){
count = cursor.getInt(0);
}
return count;
}
发布于 2011-06-15 08:07:57
发布于 2012-12-19 18:40:25
cursor.moveToNext();
cursor.getCount();
如果未调用moveToNext(),则可能会出现cursorIndexOutOfBoundException。
发布于 2012-07-27 02:17:45
使用字符串而不是int
String strCount = "";
int count = 0;
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
Cursor cursor = db.rawQuery(
"select count(*) from downloadedFragement where mixId=?",
new String[]{String.valueOf(mixId)});
while(cursor.moveToFirst()){
strCount = cursor.getString(cursor.getColumnIndex("COUNT(*)"));
}
count = Integer.valueOf(strCount).intValue();
https://stackoverflow.com/questions/6351606
复制相似问题