5

I have a content provider that accesses my database which is fine if you need to deal with record sets but I need a method to return an integer denoting the number of records in a table

The method looks like this

public long getRecordCount(String TableName) {
    SQLiteDatabase mDatabase = mOpenHelper.getReadableDatabase();
    String sql = "SELECT COUNT(*) FROM " + TableName;
    SQLiteStatement statement = mDatabase.compileStatement(sql);
    long count = statement.simpleQueryForLong();
    return count;
}

But I am unable to find any way of using this (Or any other method that does not return a cursor for that matter) in a content provider so where is the best place to put this method and how to call it?

Obviously I could do the really bad option of selecting all the records with a managed query and using the cursor.count result but that is one hugely inefficient way of dealing with this specific requirement

Thanks

jamesc
  • 12,423
  • 15
  • 74
  • 113

2 Answers2

10

You can also simply use "count(*)" as a projection in a call to your content providers URIs, as in the following helper method

public static int count(Uri uri,String selection,String[] selectionArgs) {
  Cursor cursor = getContentResolver().query(uri,new String[] {"count(*)"},
      selection, selectionArgs, null);
  if (cursor.getCount() == 0) {
    cursor.close();
    return 0;
  } else {
    cursor.moveToFirst();
    int result = cursor.getInt(0);
    cursor.close();
    return result;
  }
}
mtotschnig
  • 1,238
  • 10
  • 30
1

One way you can access it is by using the call() method in the ContentResolver class. I can't seem to find much about how to actually use this on google, but my guess is that you should just have your getRecordCount() return a bundle with your result in it. Of course the easier thing to do would be something like what's described in this SO Post.

Community
  • 1
  • 1
Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • Thank you, this opens up a number of other questions which would be best answered if there was an example of how to implement your suggested solution. Particularly how to get the value from the returned bundle and how to put the value into the bundle class result. If you could point me in the direction of a usage example of this method I would really appreciate it – jamesc Nov 07 '11 at 20:38
  • I'm having trouble finding exactly how to use the method as well. See my edited response for some other suggestions. – Kurtis Nusbaum Nov 07 '11 at 20:53
  • lol! Not on my own then :) - I guess I can use a managed query, set up a new uri and have the result of the select count * return a cursor - Seems like the simples solution. Thanks for your help – jamesc Nov 07 '11 at 21:25
  • From what I can tell from browsing the [android source code](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/content/ContentProvider.java#ContentProvider.call%28java.lang.String%2Cjava.lang.String%2Candroid.os.Bundle%29), [call()](http://developer.android.com/reference/android/content/ContentResolver.html#call%28android.net.Uri,%20java.lang.String,%20java.lang.String,%20android.os.Bundle%29) will always return null. – IT-Dan Sep 24 '12 at 06:46