Android
Data objects

Relational Queries

9min

Relational Queries on Android

Introduction

In this guide, you will perform relational queries in Parse and implement an Android app using these queries. You will learn how to set up and query realistic data using Back4App and Android.

This tutorial uses an app created in Android Studio Arctic Fox -2020.3.1 with compileSdk = 30 , minSdk = 23 and targetSdk = 30

At any time, you can access the complete Android Project built with this tutorial at our Github repositories

Goal

Our goal is query data stored on Back4App from an Android app.

Here is a preview of what we are gonna achieve:

Document image


Prerequisites

To complete this tutorial, we need:

Let’s get started!

Before next steps, we need to connect Back4App to our application. You should save the appId and clientKey from the Back4App to string.xml file and then init Parse in our App.java or App.kt file. Follow the New Parse App tutorial if you don’t know how to init Parse to your app.

Or you can download the projects we shared the github links above and edit only the appId and clientKey parts according to you.

At any time, you can access the complete Android Project built with this tutorial at our Github repositories

1 - Save some data on Back4App

In this step, we will create a Class with the JS Console and Javascript codes provided by Parse and we will create queries for this Class.

Let’s create an assortment of classes, which will be the target of our queries in this guide. The classes are: Author, Book, Publisher and BookStore, in which Book has a 1:N relation with Publisher and N:N with Author, and BookStore has an N:N relation with Book.

On Parse JS Console is possible to run JavaScript code directly, querying and updating your application database contents using the JS SDK commands. Run the code below from your JS Console and insert the data on Back4App. Here is how the JS console looks like in your dashboard:

Document image


Go ahead and create the classes with the following example content:

Java


2 - Query the data from Android app

Now that you have populated all the classes, we can now perform some relational queries in it. Let’s begin by filtering Book results by the publisher, searching for the ones that belong to the Publisher “Acacia Publishings” (or “Publisher A”) using the basic ParseQuery.equalTo method:

Java
Kotlin


Let’s now query which BookStore objects contain Book objects with publishing date greater than 01/01/2010, using an inner query with the ParseQuery.whereGreaterThan method and then the ParseQuery.whereMatchesQuery method:

Java
Kotlin


Now lets create a more complex relational query, looking for BookStore objects that have at least one Book written by Author “Aaron Writer” (or “AuthorA”), using whereEqualTo and whereMatchesQuery:

Java
Kotlin


It’s done!

At the end of this guide, you learned how relational queries work on Parse and how to perform them on Back4App from an Android App.

Updated 28 Mar 2024
Did this page help you?