diff options
| author | Christian Mehlmauer <FireFart@gmail.com> | 2010-06-16 22:56:07 +0200 |
|---|---|---|
| committer | Jean-Baptiste Queru <jbq@google.com> | 2010-08-20 18:02:09 -0700 |
| commit | e7731f0a7f824add1fffa391965cbfeaf7cb2cf2 (patch) | |
| tree | 35986fdc88135d1a7789461121ad0f24b1398fa2 /core/java/android/database/DatabaseUtils.java | |
| parent | ec58dff06693342f12f295fdb7b41cfcbaeb4f14 (diff) | |
Added overload methods for DatabaseUtils.queryNumEntries
Now you can filter the count statement with a selection
and selection args
UnitTests for this new methods are added to the cts project
Change-Id: Id9233aec0eaac08839041ae7cbaba203470ad3d8
Diffstat (limited to 'core/java/android/database/DatabaseUtils.java')
| -rw-r--r-- | core/java/android/database/DatabaseUtils.java | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/core/java/android/database/DatabaseUtils.java b/core/java/android/database/DatabaseUtils.java index 9bfbb74eeb27..78e757ca441a 100644 --- a/core/java/android/database/DatabaseUtils.java +++ b/core/java/android/database/DatabaseUtils.java @@ -50,8 +50,6 @@ public class DatabaseUtils { private static final boolean DEBUG = false; private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV; - private static final String[] countProjection = new String[]{"count(*)"}; - /** * Special function for writing an exception result at the header of * a parcel, to be used when returning an exception from a transaction. @@ -604,14 +602,40 @@ public class DatabaseUtils { * @return the number of rows in the table */ public static long queryNumEntries(SQLiteDatabase db, String table) { - Cursor cursor = db.query(table, countProjection, - null, null, null, null, null); - try { - cursor.moveToFirst(); - return cursor.getLong(0); - } finally { - cursor.close(); - } + return queryNumEntries(db, table, null, null); + } + + /** + * Query the table for the number of rows in the table. + * @param db the database the table is in + * @param table the name of the table to query + * @param selection A filter declaring which rows to return, + * formatted as an SQL WHERE clause (excluding the WHERE itself). + * Passing null will count all rows for the given table + * @return the number of rows in the table filtered by the selection + */ + public static long queryNumEntries(SQLiteDatabase db, String table, String selection) { + return queryNumEntries(db, table, selection, null); + } + + /** + * Query the table for the number of rows in the table. + * @param db the database the table is in + * @param table the name of the table to query + * @param selection A filter declaring which rows to return, + * formatted as an SQL WHERE clause (excluding the WHERE itself). + * Passing null will count all rows for the given table + * @param selectionArgs You may include ?s in selection, + * which will be replaced by the values from selectionArgs, + * in order that they appear in the selection. + * The values will be bound as Strings. + * @return the number of rows in the table filtered by the selection + */ + public static long queryNumEntries(SQLiteDatabase db, String table, String selection, + String[] selectionArgs) { + String s = (!TextUtils.isEmpty(selection)) ? " where " + selection : ""; + return longForQuery(db, "select count(*) from " + table + s, + selectionArgs); } /** |
