开发者

how to get count column

开发者 https://www.devze.com 2022-12-30 04:22 出处:网络
I\'d like to get the value of Count column from cursor. public Cursor getRCount(String iplace) throws SQLException

I'd like to get the value of Count column from cursor.

public Cursor getRCount(String iplace) throws SQLException
{
 try {
      String strSql = "SELECT COUNT(_id) AS RCount FROM tbName WHERE place= '" + iplace + "'";            
      return db.rawQuery(strSql, null);
    } ca开发者_Go百科tch (SQLException e) {
        Log.e("Exception on query", e.toString());
        return null;
    }
} 

I tried to get this count column value from cursor like below

Cursor cR = mDbHelper.getRCount(cplace);if (cR.getCount() > 0){long lCount = cR.getLong(0);}cR.close();}

I got the debug error. How to get it?

PS: Could I use nested cursors?


You should use DatabaseUtils.longForQuery utility method to run the query on the db and return the value in the first column of the first row.

int sometotal=DatabaseUtils.longForQuery(db,"SELECT COUNT(_id) AS RCount FROM tbName WHERE place= '" + iplace + "'",null);


The Cursor is positioned before the first row when you get it back from rawQuery(). Call moveToFirst() before attempting to use the Cursor, and your problem should go away.

0

精彩评论

暂无评论...
验证码 换一张
取 消