SQL Database for beginners — Part 2

Nikhil Bansal
MindOrks
Published in
4 min readAug 9, 2017

This is the 2nd Part of the “SQL Database for beginners ” series. It is in direct continuation to Part 1 so I recommend you read it first before continuing.

In last post we created Contract and Helper class for our database that were used to define the schema of the table and create the table. In this part we will setup the ContentProvider for our database.

A Content Provider manages access to a central repository of data. Content providers are primarily intended to be used by other applications, which access the provider using a provider client object. It manages access to a structured set of data by handling requests from other applications.

Abstract class ContentProvider defines six abstract methods that you must implement as part of your own concrete subclass.

  1. onCreate() : Initialize your provider. Android system calls your this method immediately after it creates your provider.
  2. insert() : Insert a new row into your provider. Use the arguments to select the destination table and to get the column values to use. Return a content URI for the newly-inserted row.
  3. query() : Retrieve data from the content provider. Use the arguments to select the table to query, rows and columns to return and sort order of the result. Return data as cursor object.
  4. update() : Update existing rows in your provider. Use the arguments to select the table and rows to update and to get the updated column values. Return the number of rows updated.
  5. delete() : Delete existing rows from your provider. Use the arguments to select the table and rows to delete. Return the number of rows deleted.
  6. getType() : Return the MIME type corresponding to a content URI. It is used by applications to retrieve the MIME type of the given content URI. If your app isn’t concerned with the data’s MIME type, it is perfectly fine to return null .

Define UriMatcher

Firstly, an instance of UriMatcher is created and then the URIs of table and row are added in the matcher using the addUri() method which takes in 3 arguments :

  1. Authority : It serves an an Android-internal name.It is used to uniquely define your app’s content provider.
  2. Path : It is the path that is used to access your table or the row of table.
  3. Code : It is used to map the URI to a unique integer that is used later on to match whether URI is for the table or a particular row.

Define all the Methods

Next, all the six methods should be defined.

In the onCreate() method make an instance of helper class is important as it will be used to get a reference to the SQL database which will be used to retrieve data from database or modify it.

In the query() method, firstly we need a reference to a readable database so call getReadableDatabase() method on the helper instance. Then match the Uri received with the UriMatcher instance so that we know that the user needs the complete table or a row of table. Th en use query() method on the database instance to get the results and save it in a cursor object. The query() method takes in 7 arguments :

  1. Table Name : Name of the table to get data from.
  2. Projection : It is a String array which contains column names that the user needs
  3. Selection : It is a string which acts as a where clause so that we can query a particular row of the table.
  4. SelectionArgs : It contains the row number the user needs.
  5. GroupBy : Used to group the results according to a particular column.
  6. Having : Used to apply having clause to the result.
  7. OrderBy : Used to order the result either in ascending or descending order.

In the delete() method firstly we need the reference to a database. So call getWritableDatabase() on the helper instance. Notice that we called getWritableDatabase() instead of getReadableDatabase() method we used before. This is because query() method does not change anything in the database but the delete method is used to modify the database. Then you just have to match the uri and call delete() method on the database reference.

The update() and insert() method are defined in the same manner as delete() method. In getType() method, first the uri is checked using the matcher instance and then the mime type string is returned which we created in the contract class.

Here is the snippet of DataContentProvider.class :

At last you have to declare your content provider in the AndroidManifest file.It is done using <provider> tag in which 2 attributes are necessary :

  1. android:authorities : Enter your authority here so that the manifest knows about your content provider’s authority
  2. android:name : Give the name of the Content Provider class along with the package in which it is defined.

This post completes the setting up of the SQL Database. Hope it helps you to be comfortable with setting up an database for your app

If you liked it, show some love❤ and share more! Keep practicing and happy coding!

--

--