active android show all

Android Database Without SQLite Part 1: Using ActiveAndroid Library Basics

This is our first post of our 4 PART series that talks about how to create a database in Android using ActiveAndroid library without using SQLite.

Download the Source Code Here

Steps

  1. Download ActiveAndroid .jar from here [or Google it if this link is broken].
  2. Add this library to the libs folder of your newly created/existing project.
  3. Create a class that represents your database table called as Model in this library.
  4. Perform CRUD operations.

1 Download and Activate the library ‘Active Android’

First, let’s create a new project in Eclipse. You may use an existing project if you want.

active android new project

slidenerd

Let’s finish creating our project. We’ll keep the layouts and structure of the app relatively simple since our main purpose here is to understand how this library works.

active android blank activity

slidenerd

Now, lets download our library from here

Copy your downloaded jar file and paste it inside libs folder in Eclipse to make it look like this.

active android copy jar file to lib folder

slidenerd

 

Right click on that newly added jar file, select the option ‘Build Path’  and select the option ‘Add to Build Path’ .  Now right click on your project in the Left Pane of Eclipse and select ‘Properties’ and navigate to the screen shown below.

active android project properties

slidenerd

Be sure to check our library’s jar file and click ‘OK’ to finish. We have integrated the library with our project at this point and before we are ready to use it, we need to perform some steps.

2 Configure your Project

Add the database name and database version that you would like to create within the AndroidManifest.xml as shown below

We require an Application class that does initialization of Active Android functionality. So lets make an Application class if we don’t have one or add this code inside the onCreate() of our existing Application class.

 3 Create a Table

Initially, let’s try to work with a single table and find out how it works. The table’s actual structure looks as shown below. This table structure will be modified in the upcoming posts to add more columns or add new tables as needed.

active android create table person

For each table, we need to create a separate class that extends Model from the library. Let’s create a separate package where we’ll add more Model classes as we proceed further.

active android create model

We create a class Person that is a subclass of Model. This class will define instance variables such as name, age and these instance variables represent the columns in our table. Let’s add the instance variables in code.

To finish creating our table, all we need to do is add appropriate annotations to the instance variables personName and personAge to tell ActiveAndroid that they are the columns in our table and add an annotation to our class Person to tell ActiveAndroid that it is our table. Notice, we haven’t created an _id field since ActiveAndroid manages this for us. Let’s add the annotations @Table(name=”<name of the table you want here>”) and @Column(name=”<name of the column you want here>”) to our code. Notice how we call the super() constructor to initialize our Model subclass.

 4 Create our UI to save Person details

Here’s our activity_main.xml that lets you add the person’s name, age and save it to our database. We have used certain drawables for Button and EditText but you can find the full source code right here.

active android user interface

4 Saving Data to the Database

  1. Get the data entered by our user from both the EditText.
  2. Create an object of our Model class Person.
  3. Call save() on it.

 5 Displaying All the Data from our Database for our Table Person

  1. Create an object of Select that represent a select query in this library.
  2. Call select.all() using the above created object to select all rows.
  3. Specify the table using .from() and call execute() to execute the query.
  4. It returns an ArrayList of our Model subclass.
  5. Process the ArrayList
We added a few rows first and then executed our query to display all the results and guess what we got? If the app crashes or if you see null results, just uninstall this app and re-install it again and it works like a charm!

active android show all

 

Conclusion

We have just touched the surface of what is possible with this library. In the next post, we will explore relationships, foreign key constraints and other query aspects with this library. In the meantime , stay tuned with slidenerd and let intelligence be your only keyword.