android database without sqlite code tables person and score

Android Database Without SQLite Part 2: Multiple Tables with ActiveAndroid library

In the previous post, we discussed about how we can create a simple database in Android using a library called ActiveAndroid that lets us create databases without using SQLite code. Here, we take a look at some of the advanced stuff we can do with this library such as implementing Multiple tables with Foreign Key constraints, performing queries, performing bulk operations and so on. If you haven’t read the previous post it’s right HERE.

Download the Source Code Here

What do we discuss in this article?

  1. Create 2 tables Person and Score. The Person Table has 4 columns [_id, personName, personAge, personScore which actually refers to an ID from the Score table]. The Score table has 5 columns [_id, scorePhysics, scoreChemistry, scoreMaths, scoreBiology].
  2. Modify the previously designed UI to add ability to insert score values.
  3. Save the values of both Person and Score.
  4. Perform a simple query on this table.
android database example schema

android database example schema


Step 1: Create the Model classes for Person and Score

  1. Create the Score class if you haven’t done it so far.
  2. Let the Score class extend Model from the library.
  3. Add instance variables int scorePhysics, int scoreChemistry, int scoreMaths, int scoreBiology.
  4. Add 2 constructors, 1 empty and 1 parameterized and ensure both call super() in the first statement to do forward the super class initialization work.
  5. Add @Table(name=”Score”) annotation to your class to indicate it forms a table with the specified name.
  6. Add @Column(name=”personPhysics”)…and so on to each instance variable to indicate it forms a column inside the table with the specified name.
  7. Add a toString() as shown below method since we plan to display the score object later.

Here is how, the Score class looks at this point.

Now, its time to modify the Person class which we made in the previous post to include the Score object as well. The _ID primary key of the Score object that will be generated by ActiveAndroid library is used as a foreign key inside the Person table.

  1. Modify the Person table to include an instance variable of type Score.
  2. Add the @Column annotation for this instance variable.
  3. Modify the constructor appropriately.

Here is how the Person class looks now.

 Step 2: Modify the UI to add score values

Compared the to the previous post, we have made some changes to the UI. It uses a LinearLayout with a vertical orientation as its root now.

android database without sqlite activity_main.xml

This is the modification we have done to our layout file.

And here is the full code of our layout.

 Step 3: Save the values of both Person and Score

You may need to uninstall and reinstall this app after completing this step. The reason? The database structure for Person has changed and saving the score may not work unless you do so.

  1. Get the data from the EditText for all our scores
  2. Convert it to an Integer.
  3. Create a new Score object with these 4 values.
  4. Call save() on our new Score object.
  5. Create a Person object with the name and age extracted from their respective EditText and our new score object.
  6. Call save() on the Person object.

Inside our, this is how the method for saving data looks like now.

The full code of our looks as shown below.

 Step 4: Perform a simple query on this table.

Well, we won’t have to make any major modification to display all the data. If you observe the showAll() method inside our, you’ll notice that we have performed a minor change when it comes to the StringBuilder to display the score object which is also why we added a toString() method inside the Score class.

android person and score table queries