Saturday, November 5, 2011

Simple Content Provider for db operations

At last I concluded, it is ContentProvider which we need to use for database operation ..
Content Providers  
Content providers store and retrieve data and make it accessible to all applications. They're the only way to share data across applications; 

  • Yes content providers are mainly aimed to share data amount different application
  • It can be also used inside our single application
  • There is no worries of closing and locking of db
  • It can be accessed from different activities,  services and Thread. Anywhere that have context object
  • I don't know is this a bad idea, I don't think so. Don't think it will reduce our data security. Our data can be accessed only with our AUTHORITY url
When a request is made via a ContentResolver the system inspects the authority of the given URI and passes the request to the content provider registered with the authority.
Now I'm trying to demonstrate the way how we can use ContentProvider in most simple way...

public class ContentProviderDb extends ContentProvider{
 OurDatabaseHelper dbHelper ;
 public static final String AUTHORITY = "ourContentProviderAuthorities";//specific for our our app, will be specified in maninfed 
 public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
 public boolean onCreate() {
  dbHelper = new OurDatabaseHelper(getContext());
  return true;

 public int delete(Uri uri, String where, String[] args) {
  String table = getTableName(uri);
     SQLiteDatabase dataBase=dbHelper.getWritableDatabase();
     return dataBase.delete(table, where, args);

 public String getType(Uri arg0) {
  // TODO Auto-generated method stub
  return null;

 public Uri insert(Uri uri, ContentValues initialValues) {
  String table = getTableName(uri);
  SQLiteDatabase database = dbHelper.getWritableDatabase();
  long value = database.insert(table, null, initialValues);
  return Uri.withAppendedPath(CONTENT_URI, String.valueOf(value));

 public Cursor query(Uri uri, String[] projection, String selection,
      String[] selectionArgs, String sortOrder) {
  String table =getTableName(uri);
  SQLiteDatabase database = dbHelper.getReadableDatabase();
  Cursor cursor =database.query(table,  projection, selection, selectionArgs, null, null, sortOrder);
     return cursor;

 public int update(Uri uri, ContentValues values, String whereClause,
      String[] whereArgs) {
  String table = getTableName(uri);
  SQLiteDatabase database = dbHelper.getWritableDatabase();  
  return database.update(table, values, whereClause, whereArgs);
 public static String getTableName(Uri uri){
  String value = uri.getPath();
  value = value.replace("/", "");//we need to remove '/'
  return value;

Here OurDatabaseHelper is the database helper class (extends SQLiteOpenHelper )
getTableName is used to get the specific Table name

Don't forget to add this in your manifest.xml file
<provider android:authorities="ourContentProviderAuthorities"
We specify our AUTHORITY as android:authorities

So the db operation will be like this:

Uri contentUri = Uri.withAppendedPath(ContentProviderDb.CONTENT_URI, "TableName");
Using these contentUri we are specifying our table where we need to perform our operation

We can perform db operation using our context


And if you are inside an activity class


For an example an insert operation will look like this

ContentValues initialValues = new ContentValues();
initialValues.put("Column", "value");
Uri contentUri = Uri.withAppendedPath(ContentProviderDb.CONTENT_URI, "TableName");
Uri resultUri = context.getContentResolver().insert(contentUri, initialValues);

Hope this help.
And please feel free to point out my mistakes...