首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用SQLite在Android中获取查询行数?

如何使用SQLite在Android中获取查询行数?
EN

Stack Overflow用户
提问于 2011-06-15 08:06:06
回答 7查看 53.1K关注 0票数 51

如何使用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;
}    
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2011-06-15 08:07:57

票数 107
EN

Stack Overflow用户

发布于 2012-12-19 18:40:25

cursor.moveToNext();
cursor.getCount();

如果未调用moveToNext(),则可能会出现cursorIndexOutOfBoundException。

票数 8
EN

Stack Overflow用户

发布于 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();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6351606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档