How to get column count

I would like to get the value of the Count column from the 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); } catch (SQLException e) { Log.e("Exception on query", e.toString()); return null; } } 

I tried to get this count column value using the cursor as below

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

I got a debugging error. How to get it?

PS: Can I use nested cursors?

+3
android cursor
May 12 '10 at 9:20
source share
2 answers

You should use the DatabaseUtils.longForQuery method to start the query on 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); 
+9
May 12 '10 at 11:31
source share

Cursor rawQuery() first row when you return it from rawQuery() . Call moveToFirst() before trying to use Cursor , and your problem should go away.

0
May 12 '10 at 10:34
source share



All Articles