What is the use of BaseColumns in Android

The BaseColumn interface only provides the column names _ID and _COUNT. You must still specify columns that use them when constructing tables. For example, to create a column using the column name _ID you might do the following:

public static final String CREATE_TABLE =
    "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
    + _ID + " INTEGER PRIMARY KEY, "
    + USERNAME + " TEXT NOT NULL, "
    + PASSWORD + " TEXT NOT NULL, "
    + EMAIL + " TEXT NOT NULL UNIQUE)";

The BaseColumns interface provides names for the very common _ID and _COUNT columns.

Using common names enables the Android platform (and developers as well) to address any data item, regardless of its overall structure (i.e. other, non-ID columns) in a unified way. Defining constants for commonly used strings in an interface/class avoids repetition and typos all over the code.

Using a column named _id (the constant value of BaseColumns._ID) is required by CursorAdapter, implementations of a ContentProvider and other places where you hand off a Cursor to the Android platform to do things for you. For example, the adapter of a ListView uses the _ID column to give you the unique ID of the list item clicked in OnItemClickListener.onItemClick(), without you having to explicitly specify what your ID column is every time.

Whether or not to implement interfaces consisting only of constants or reference them with their full name, i.e. BaseColumns._ID is a matter of taste. I personally prefer the latter, because it's more obvious where _ID is coming from and the former feels like an abuse of inheritance.


This is a simple interface which adds two fields :

public interface BaseColumns
{
    /**
     * The unique ID for a row.
     * <P>Type: INTEGER (long)</P>
     */
    public static final String _ID = "_id";

    /**
     * The count of rows in a directory.
     * <P>Type: INTEGER</P>
     */
    public static final String _COUNT = "_count";
}

Internally sqlite databases used in Android, comes with an _id column that autoincrements and can function as a primary key. This also maps well with the ContentProviders