Backendless Support
 

Loading data objects from server with an SQL query

Backendless possesses multiple techniques for loading data objects from the persistent server-side storage. It is possible to review them here - in a list of all articles categorized by topic. Alongside the other options Backendless provides API for loading data objects using an SQL query. 

It is highly recommended to review the article SQL queries in Backendless which covers the questions of how to test queries using console beforehand.

Starting the review of using SQL query in the process of loading data objects from the server it is worth considering the following two data tables: 

  1. The table with 2 Person object.

 

  2.  The table with 3 Address objects.

NOTICE: The example assumes a one-to-many relation between Person and Address objects.

Suppose it is necessary to load all persons who are older than 21 and live in New York city. The query expresses exactly that condition is as following:

age > 25 and addresses.city = 'New York'

IMPORTANT: the query references object properties/columns to define the search expression.

The addresses column in the Person table references a collection of the Address objects. Since Address has the city column, it is identified using the dot notation in the query.

The code below demonstrates the query in action.

Asynchronous API sample (Android and Plain Java):
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause( "age > 25 and addresses.city = 'New York'" );
QueryOptions queryOptions = new QueryOptions();
queryOptions.addRelated( "addresses" );
dataQuery.setQueryOptions( queryOptions );
Backendless.Data.of( Person.class ).find( dataQuery, new AsyncCallback<BackendlessCollection<Person>>()
{
 @Override
 public void handleResponse( BackendlessCollection<Person> nycPeople )
 {
 Iterator<Person> iterator = nycPeople.getCurrentPage().iterator();
 while( iterator.hasNext() )
 {
 Person person = iterator.next();
 System.out.println( "Name - " + person.name );
 System.out.println( "Age - " + person.age );
 System.out.println( "City - " + person.addresses.get( 0 ).city );
 }
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
 }
} );
Synchronous API sample (Plain Java only):
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause( "age > 25 and addresses.city = 'New York'" );
QueryOptions queryOptions = new QueryOptions();
queryOptions.addRelated( "addresses" );
dataQuery.setQueryOptions( queryOptions );
BackendlessCollection<Person> nycPeople = Backendless.Data.of( Person.class ).find( dataQuery );
Iterator<Person> iterator = nycPeople.getCurrentPage().iterator();
while( iterator.hasNext() )
{
 Person person = iterator.next();
 System.out.println( "Name - " + person.name );
 System.out.println( "Age - " + person.age );
 System.out.println( "City - " + person.addresses.get( 0 ).city );
}
Program output:
Name - Frankie
Age - 30
City - New York

The API is designed in a way that allows to combine query-based search with sorting, loading of relations, paging and various other data search-related features of Backendless Service.

Review related topic:

Is article helpful?