Android Database Example For Beginners

You need the database if you want to store your data for long period of time. Android mobile uses the simple lightweight in-build database called SQLite. You don’t need to configure parameters for the android database like we do for other relational databases such as  MySql or Oracle because the database is used inside the device itself and not on some backend server.

Android developers especially beginners find it very hard to work on the database using the API provided by the android library. Hence, in this tutorial, I am going to discuss how can we save data in the database using the ActiveAndroid library. ActiveAndroid allows you to save and retrieve SQLite database records without even writing a single SQL statement. You can save, retrieve, create, update the database by calling simple methods like save() and delete(). ActiveAndroid will take care of all messy code behind the scene. You just need to configure few things before performing database operations.

Getting started

  • Create a new Android Studio project Database Example.

Installing ActiveAndroid in your project

  • Expand your Gradle Scripts folder and open build.gradle(Module:app) file. Add the following code above the dependencies{ } block.
  • Inside your dependencies{ } block, add the following code
  • Now, click on “Sync Now” Link which appears on top right of your current workspace window and then wait for the project to sync completely.

Configuring your project

  • Open the AndroidManifest.xml file from the root directory of your project. Let’s add some parameters
  • Your AndroidManifest.xml now should look like this

    Note: Application name points to the ActiveAndroid application class. This step is required for ActiveAndroid to work. If you’re already doing this to utilize another library, then simply initialize ActiveAndroid in the Application class, as shown below

Creating your database model

  • Right click on your package and click on New>Java Class, then type Book and press enter.
  • Add the following code to class
  • Add the below import statement
  • For saving the data of class, we need to set table name and column name in the above code.
    Setting table name

    Table name can be set by using java annotation just above your class name as shown below

    Setting column name

    Column name can be set using java annotation just above your variable name as shown below

  • You need to import two annotation classes for using above code

  • After setting these parameters, your should look like this

    Note: ActiveAndroid creates an id field for your tables. This field is an auto-incrementing primary key.

    Do not use the reserved keywords as the table and column names.

    Click to know more about reserved keywords.

Saving to the database

Now, we are ready to save records to the database. Let’s do it.

Inserting and updating data

  • In your, create a new object of, set the values and call the save() method. save() method can be used to both insert and update records.
  • Your class will now look like this

Viewing the saved records

  • Use the following code to view all the saved records
  • To filter the result by the author, add the following method

Deleting the record

  • To delete the book written by the author, add the following method

Modify your layout file of MainActivity and add the following code


Whole code


Android database example screen
Android database example screen

Look awesome right!. I felt the same at first. Enjoy!.
For more help, visit ActiveAndroid git documentation.
Download the source code.

Leave a Reply

Your email address will not be published. Required fields are marked *