Skip to content Skip to sidebar Skip to footer

How To Use _COUNT In BaseColumns

I've been reading up on BaseColumns](https://developer.android.com/reference/android/provider/BaseColumns.html) in Android to help structure my database schema. I know that _ID is

Solution 1:

In the database, there is nothing special about either _id or _count.

Your queries return an _id or _count column when the table is defined to have such a column, or when the query explicitly computes it.

Many objects of the Android framework expect a cursor to have a unique _id column, so many tables define it.

In most places, the _count is not expected to be present, so it is usually not implemented. And if it is actually needed, it can simply be computed with a subquery, like this:

SELECT _id,
       [other fields],
       (SELECT COUNT(*) FROM MyTable) AS _count
FROM MyTable
WHERE ...

If you want to find out the size of your own table, you are not required to use the _count name; you can execute a query like SELECT COUNT(*) FROM subjects, or, even simpler, use a helper function that does this for you.


Post a Comment for "How To Use _COUNT In BaseColumns"