db 작업할 때, 중복 row 는 제외하고 순수한 개수를 얻오올때 사용하는 방법입니다.

 

아래는 Android SDK 에서 가져온 내용입니다.

query 에 여러 매개변수 형이 있는데, 그 중에 아래의 형태를 사용하면 됩니다.

이 때, distinct 변수를 true로 하게 되면 중복 row 는 제거해줍니다.

따라서 columns 에 특정한 column name을 적어주게 되면 해당 column 에 대해 중복되지 않은 총 개수를 얻을 수 있습니다.

 

아래와 같이 RAW_ID 에 대해 중복값을 제거하면 아래와 같이 되겠네요

Cursor cr = getWritableDatabase().query(true, getDbTableName(), new String[]{RAW_ID},null,null,null,null,null,null);
/**
* Query the given URL, returning a {@link Cursor} over the result set.
*
* @param distinct true if you want each row to be unique, false otherwise.
* @param table The table name to compile the query against.
* @param columns A list of which columns to return. Passing null will
* return all columns, which is discouraged to prevent reading
* data from storage that isn't going to be used.
* @param selection A filter declaring which rows to return, formatted as an
* SQL WHERE clause (excluding the WHERE itself). Passing null
* will return 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.
* @param groupBy A filter declaring how to group rows, formatted as an SQL
* GROUP BY clause (excluding the GROUP BY itself). Passing null
* will cause the rows to not be grouped.
* @param having A filter declare which row groups to include in the cursor,
* if row grouping is being used, formatted as an SQL HAVING
* clause (excluding the HAVING itself). Passing null will cause
* all row groups to be included, and is required when row
* grouping is not being used.
* @param orderBy How to order the rows, formatted as an SQL ORDER BY clause
* (excluding the ORDER BY itself). Passing null will use the
* default sort order, which may be unordered.
* @param limit Limits the number of rows returned by the query,
* formatted as LIMIT clause. Passing null denotes no LIMIT clause.
* @return A {@link Cursor} object, which is positioned before the first entry. Note that
* {@link Cursor}s are not synchronized, see the documentation for more details.
* @see Cursor
*/
public Cursor query(boolean distinct, String table, String[] columns,
String selection, String[] selectionArgs, String groupBy,
String having, String orderBy, String limit) {
return queryWithFactory(null, distinct, table, columns, selection, selectionArgs,
groupBy, having, orderBy, limit, null);
}