Note Making Application using Sqlite vs Room : Part 1 — Using Sqlite

Anuj Gupta
3 min readJan 15, 2018

--

This tutorial is a two part series and also has a two way purpose. The first is to show the difference between using Sqlite for data persistence and using Room library for data persistence. The second is to show how to use Room Library in your project.

To show the difference between the two we are going to create a note making application. The layout for both the application is going to be exactly the same so that the only difference is in the data storage.

In the first part we are going to use Sqlite to store our notes. So let’s get started

1.Create a new project

2. Add the following dependencies in your build.gradle file

3. Create two activities

  1. MainActivity — This activities display our notes in a list
  2. AddNoteActivity — This activity is used to create new notes

and three packages

  1. models
  2. adapters
  3. database

4. Modify your activity_main.xml and activity_add_note.xml as follows

5. In the models package create a new java class called Note.java and modify it as follows

As you can see in our activity_main.xml we are using a recycler view. To display data in our recycler view we need two things . First a layout for each row in our recycler view and second an adapter to feed the recycler view with the data.

6. Create a new layout file called row_note.xml

7. Create a new class in your adapters and name it NotesAdapter and modify it as follows

8. Now comes the most important part of the tutorial — Storing data using Sqlite

1.Creating the Database

2.Creating Tables

3. Creating methods to perform

We will create a new java class in our database package called DatabaseHandler.java

DatabaseHandler class extends SqliteOpenHelper and uses SqliteDatabase Object to perform queries and return results.

Now all we need to do is use these methods in our activities

9. Modify MainActivity.java and AddNoteActivity.java as follows

Also to save the note i am creating a new menu resource file called menu_add _note.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_save"
android:title="Save"
android:icon="@drawable/ic_add"
app:showAsAction="always"
/>
</menu>

Now you can run the app and voila the new note gets added to the list.

If there is any question or confusion regarding the tutorial . Feel free to ask your questions in the comments below.

The full source code is available on github :

In Part 2 of this tutorial we will have a look at how to create a note making application using Room Library.

Thanks for reading this article. Be sure to clap/recommend as much as you can and also share with your friends. It means a lot to me.

Also, Let’s become friends on Twitter, Linkedin and Github.

--

--