Skip to content
Advertisement

How can I store data with nested object data in Android? [Room]

I have a high dimensional dataset that it provides with a json structure (about 3000 objects, an array of each object and dozens of objects in that array.) I read them with the help of the volley library. I want to save this data. in the database and access it easily. how can I do it

I couldn’t find examples for Room

JSON Data

JavaScript

Advertisement

Answer

For SQLite

The first thing that you have to do is determine the schema of the database.

Looking at your data you have a lots of MyData objects, each of which will have 0 or more Unit objects.

Thus you could have a table for the MyData objects and table for the Unit objects. Each Unit will have a parent MyData object. So in addition to the data for each Unit you could have a column that references (maps, associates) the MyData (probably it’s food_id assuming that this uniquely identifies the MyData).

Using a shortened version of MyData (just the food_id, food_name) and a shortened version of Unit (unit, amount and calory) to demonstrate then you could have two classes as:-

JavaScript

and

JavaScript

Note the constants that will be used for the tables i.e. the schema. These are placed into the classes for brevity, they could be coded elsewhere.

Note the additional column parent_food_id, this will be used to reference the parent food_id.

So the schema will(could) look like

  1. Table _mydata which has 2 columns (easy to add more)
    1. food_id (which will be unique (the PRIMARY KEY) and as the value is an integer then INTEGER type if SQLite), and
    2. food_name (as it is a String then TEXT type in SQLite).
  2. Table _unit which has 4 columns
    1. _unit (as it is a String then TEXT type in SQLite)
    2. _amount (as it is a floating point type then REAL in SQLite)
    3. _calory (as it is a floating point type then REAL in SQLite)
    4. parent_food_id as it refers to the food_id which is INTEGER then it will be INTEGER.
      1. As it’s what creates the relationship between the 2 tables then it should be a requirement that it does actually reference a food_id. Although not required it is a good idea to enforce the integirty of the reference (referential integrity) for this a Foreign Key constraint could be added.
    • It is always wise to have a primary key but as other MyData objects may have the same value for the units and or the amount and or the calory, it is probably unlikely that the combination of all three will be the same. So the primary key is all 3 columns.

In SQLite terms the SQL’s (one for each table) that will create the above could be :-

  • CREATE TABLE _mydata(_food_id INTEGER PRIMARY KEY,_food_name TEXT), and
  • CREATE TABLE _unit(_unit TEXT,_amount REAL,_calory REAL,parent_food_id INTEGER REFERENCES _mydata(_food_id) ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY(_unit,_amount,_calory))

Handling the Database

With Android and SQLite then the typical way is to utilise a DatabaseHelper that extends the SQLiteOpenHelper. SQLiteOpenHelpr requires 2 overidden methods onCreate and onUpgrae

  • onCreate runs when the database is created.
    • It runs once only for the existence of the database which persists i.e. it will only run when the App is first run.
    • this method is typically used to define the entities/components such as tables.
  • onUpgrade runs of the database version passed to the SQLiteOpenHelper is greater than the version number extracted from the database itself.

Typically code for accessing the database such as adding, removing, updating, deleting or extracting data.

For the demonstration the Database will will just have methods for inserting data into the tables. Additionally a method that will take a JSON String (equivalent of a file) and populate the tables.

So the DatabaseHelper class could be :-

JavaScript

Demonstration

Here’s a demonstration of using the above in an activity, namely MainActivity:-

JavaScript

Results

When run the log includes :-

JavaScript

and

JavaScript

Using Android Studio’s App Inspection the database looks like :-

enter image description here

and

enter image description here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement