An example of SQLite Database Part 1

How to make Note App in Android Studio

Photo by NeONBRAND on Unsplash

Here is the demo video for this article. So that you can check the final result of this tutorial if you follow from start to end.

Now I am going to design an Activity that can store notes. The design will look like this.

note input design
note input design

Here is the layout source code. I think you can easily understand the following code because it is a very simple layout design.

Let’s explain for beginners. I have taken RelativeLayout as the parent layout for this activity. Inside the relative layout, I have taken LinearLayout, align this layout in the center of the screen by applying the android:layout_centerInParent=”true”.

And Inside the LinearLayout, I have taken three EditText for taking the title, description, and date respectively, and two Buttons for saving and display the saved notes in another Activity.

Now create a package named Model where your java files should exist in android studio and right-click on the package and create a java class named note_model.

So in this note_model class, we need to declare four private variables id, title, description, and date, and then make two constructors for this java class. The first one should be the default constructor without any parameters and the second one should be the parameterized constructor which will set values of all private fields.

Then create setter and getter for all private fields. The shortcut key(alt+insert) is available for creating setter and getter functions in Android studio.

So here is the note_model class.

Let’s come to the database coding for saving the notes.

Photo by Christian Bowen on Unsplash

So first we need to create a package named database then right-click on it and make TableSchema class which will store our database tables name and fields as a constant. So it is easy to call variables names than writing magic string again and again. Some programmers miss spelling the table column's name then it becomes a headache. Right?


So here is the class TableSchema class source code.

Basically, I have created a static note class inside the TableSchema class which contains constant variables and in these constant variables, I have stored table name and column names for the tbl_note table. So we can call these static constant variables where we need them. Because of static keyword, we can call them without creating the object of the classes.

So that’s the reusability.

Photo by Bluewater Globe on Unsplash

Let's Create SQLite Database Helper for our Note App

Here is the source code for the SQLite Database Helper class.

Let me explain MySqliteHelper class

This class extend with SQLiteOpenHelper class

The SQLiteOpenHelper class is coming with Android SDK. If you want to learn in detail about the SQLiteOpenHelper class then you should click here.

So After extending our MySqliteHelper class with SQliteOpenHelper class then it important to implement onCreate and onUpgrade methods which are coming from SQLiteOpenHelper class. And Also we need to create a constructor of our helper class to call the constructor of the supper class which will take care of handling database operations.

Now we need to create two private, static, and final fields in which we will specify the database name and version number for the database like this

private static final String DATABASE_NAME="note.db";
private static final int VERSION=1;

After it, we need to pass the Database name from the super constructor as a second argument and the version number as a fourth argument like in the below code.

super(context, DATABASE_NAME, null, VERSION);

OnCreate method

This method will be called after the constructor of MySQLiteHelper and in this method, we need to create tables in the database. So we need to write the query for creating the tbl_note in the database. We need to write a query in String variable then execute this query using SQLiteDatabase object which is coming from the onCreate method’s parameter like this.

public void onCreate(SQLiteDatabase sqLiteDatabase) {
String table1="create table "+TableSchema.note.TABLE_NAME+"("+TableSchema.note.ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+TableSchema.note.TITLE+" TEXT, "+TableSchema.note.DESCRIPTION+" TEXT, "+TableSchema.note.DATE+" TEXT );";

The saveNote Method

public boolean saveNote(note_model model){
SQLiteDatabase database=this.getWritableDatabase();
ContentValues cv=new ContentValues();
long id= database.insert(TableSchema.note.TABLE_NAME,null,cv);
if (id==-1){
return false;
return true;

In this method, note_model class object passed as a parameter. So in this method, I have called the getWritableDatabase() method for opening the database in writable mode and this method returns SQLiteDatabase object. before inserting the data in the table, we need to populate the ContentValues object.

So we need to create the ContentValues class object then we need to call the put method to bind values with column names then pass the ContentValues class object from the insert method as the last argument. The first argument should be a table name and the second argument will nullHacks. Normally, we pass a null value as the second argument. And in the end, it is important to close the database object using the database.close() method. insert() method will return -1 if data will not be inserted otherwise it will return the last inserted primary key column value.

First of all, you have to enable Viewbinding for the project. Using ViewBinding, We can call all the UI objects without using the findViewById() method in an activity. if you want to learn more about ViewBinding you can check this link.

So now after, the setContentView() method I have created the MySqlitehelper class so named as the helper. So that we can call the save note method to save the notes.

Now we have to create a click listener save button object. In this click listener, I’m getting the from EditText and saving the values to the model object which I have created earlier in this article. After it, I have to make validation to check user have entered all values in the fields.

After validating the fields, now we can save notes to the database. For storing the data in the database, we have to call the save note method using our SQLite helper class object and pass the model object to saveNote method. This method will return true if data is stored in the database successfully otherwise it will return false.

That's all for this part.

Thanks for reading this article till the end. We will discuss this application in the next article. The second part will come soon. I will give you the link here.

Here are more articles.

I’m a good husband, father and Senior Developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store