Skip to content
Droid Bits

Droid Bits

Notes on Android Development

Find Droid Bits

Tag: CursorLoader

CursorLoader – DEPRECATED

Categories: Java AndroidTags: Cursor, CursorLoader, Loader

CursorLoader (like all Loaders) is deprecated. Do not use.

SQLite Databases

Categories: Java AndroidTags: BaseColumns, Content, Content Authority, Content Provider, ContentResolver, Contract, CursorLoader, database, execSQL, onUpgrade, SQL, sqlite, Table, Uri, Version

SQLite databases are commonly used for on-device storage of data. Databases can be created and manipulated using the standard SQL commands. The following example uses a Contract class file to define the names of the table columns and the URI for the Content Authority, which would allow a third-party app access to the data in your database if you wished it to be a Content Provider:

Create a Contract class:

import android.content.ContentResolver;
import android.content.ContentUris;
import android.net.Uri;
import android.provider.BaseColumns;

public class ExampleContract implements BaseColumns {

    // This is the content authority for our app Content Provider.
    public static final String CONTENT_AUTHORITY = "com.example.android.ourapp";

    // This is the {@link Uri} on which all other Uris for our app are built.
    public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);

    // The path for a particular database of data e.g. 'data'
    public static final String PATH_DATA = "data";

    // This is the {@link Uri} used to get a full list of names and ages.
    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_DATA).build();


    // This is a String type that denotes a Uri references a list or directory.
    public static final String CONTENT_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TERMS;

    // This is a String type that denotes a Uri references a single item.
    public static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TERMS;


    // Declaring all these as constants makes code a lot more readable.
    // It also looks a more like SQL.

    // This is the version of the database
    public static final int DATABASE_VERSION = 1;

    // This is the name of the SQL table for data.
    public static final String DATA_TABLE = "data_entries";
    // This is the name of the SQL database for data.
    public static final String DATABASE_NAME = "data";

    // This is the column name in the SQLiteDatabase for our first column, 'name'.
    public static final String COLUMN_NAME = "name";
    // This is the column name in the SQLiteDatabase for our first column, 'age'.
    public static final String COLUMN_AGE = "age";

    // This is an array containing all the column headers in the data table.
    public static final String[] COLUMNS =
            {_ID, COLUMN_NAME, COLUMN_AGE};

    // This is the index of the ID in the data table
    public static final int COLUMN_INDEX_ID = 0;
    // This is the index of the name in the data table
    public static final int COLUMN_INDEX_NAME = 1;
    // This is the index of the age in the data table
    public static final int COLUMN_INDEX_AGE = 2;

    /**
     * This method creates a {@link Uri} for a single piece of data, referenced by id.
     * @param id The id of the term.
     * @return The Uri with the appended id.
     */
    public static Uri buildDataUriWithId(long id) {
        return ContentUris.withAppendedId(CONTENT_URI, id);
    }
}

Create a helper class to handle the SQL commands (in separate class file):

public class DataDbHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "data.db";
    private static final int DATABASE_VERSION = 1;

    public DataDbHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        /*
         * This String will contain a simple SQL statement that will create a table that will
         * cache our data.
         */
        final String SQL_CREATE_DATA_TABLE =

                "CREATE TABLE " + ExampleContract.DATA_TABLE + " (" +

                        /*
                         * Our contract did not explicitly declare a column called "_ID". However,
                         * it implements the interface "BaseColumns" which does have a field
                         * named "_ID". We use that here to designate our table's primary key.
                         */
                        ExampleContract._ID               + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        ExampleContract.COLUMN_NAME       + " VARCHAR, "                 +
                        ExampleContract.COLUMN_AGE + " VARCHAR" + ");";

        /*
         * After we've spelled out our SQLite table creation statement above, we actually execute
         * that SQL with the execSQL method of our SQLite database object.
         */
        db.execSQL(SQL_CREATE_DATA_TABLE);
    }

    /**
     * This example database is only a cache for online data, so its upgrade policy is simply to discard
     * the data and call through to onCreate to recreate the table. Note that this only fires if
     * you change the version number for your database (in our case, DATABASE_VERSION). It does NOT
     * depend on the version number for your application found in your app/build.gradle file. If
     * you want to update the schema without wiping data, commenting out the current body of this
     * method should be your top priority before modifying this method.
     *
     * @param sqLiteDatabase Database that is being upgraded
     * @param oldVersion     The old database version
     * @param newVersion     The new database version
     */

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + ExampleContract.DATA_TABLE);
        onCreate(db);
    }
}

To make use of this database use a Cursor
ud851-Sunshine-student\S07.03???