An Idiot’s guide to Android Content Providers Part 1
In this step by step tutorial we are building TODO Application database schema. This article doesn’t covers how to design the application, but explains the database design and preparing database helper classes.
- First order of business is to define our database schema.
//TODO: (1) Create a class TodoContract
//TODO: (2) Create a inner class TodoEntry that implements BaseColumns
//TODO: (3) Define the table and columns name inside TodoEntry
2. To query a content provider, you specify the query string in the form of a URI which has following format −
content://content_authority/table_name
A content URI is a URI that identifies data in a provider. Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table (a path). When you call a client method to access a table in a provider, the content URI for the table is one of the arguments.
//TODO: (1) Create a String CONTENT_AUTHORITY which will be your package name
//TODO: (2) Create a Uri BASE_CONTENT_URI which will be equal to "content://"+CONTENT_AUTHORITY
//TODO: (3) Create two Uri one for accessing all todos and one for the specific record requested
3. Now create a class TodoDbHelper
to help create database and table
//TODO: (1) Create a class TodoDbHelper that extends SQLiteOpenHelper
//TODO: (2) Override onCreate and onUpgrade method
//TODO: (3) Create a String DATABASE_NAME = "database_name.db"
//TODO: (4) Create a int DATABASE_VERSION = 1
//TODO: (5) Create a constructor with Context as a parameter
4. Initiate a CREATE TABLE
SQL query inside onCreate
and reset table in onUpgrade
We have successfully created the supporting classes that will help us to create Content Provider. Checkout part 2 for further implementation.