如何从 SQLite 获取记录总数 [英] How to get the total count of records from SQLite

查看:39
本文介绍了如何从 SQLite 获取记录总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Sqlite DB 获取总行数.以下是我正在尝试做的代码片段.

I am trying to get the total count of rows from Sqlite DB. Following is the code snippet of what I am trying to do.

我不知道我在这里做错了什么?

I dont know what I am doing wrong here ?

public static int getTotalCount(Context _context)
{
Cursor c = null;
try
{
    c = getInstance(_context).db.rawQuery("SELECT COUNT(word) FROM  "
        + WORD_TABLE, null);

    return c.getInt(c.getColumnIndex(WORD_COL));

} catch (Exception e)
{
    Log.e("getTotalCount", e.toString());
}
return 0;

异常信息:05-08 05:21:19.934:错误/getTotalCount(440):android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为1

Exception message : 05-08 05:21:19.934: ERROR/getTotalCount(440): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

推荐答案

Try c.getInt(0);

 c = getInstance(_context).db.rawQuery("SELECT COUNT(word) as WORD_COL FROM  "
    + WORD_TABLE, null);

还在 rawQuery 行之后放置一个 c.moveToFirst(); 以进行良好测量.

also put a c.moveToFirst(); right after the rawQuery line for good measure.

这篇关于如何从 SQLite 获取记录总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆