Android
Data objects
Relational Queries
9 min
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 compilesdk = 30 , minsdk = 23 minsdk = 23 and targetsdk = 30 targetsdk = 30 at any time, you can access the complete android project built with this tutorial at our github repositories kotlin example repository java example repository goal our goal is query data stored on back4app back4app from an android android app here is a preview of what we are gonna achieve prerequisites to complete this tutorial, we need android studio an app created on back4app note follow the new parse app tutorial to learn how to create a parse app on back4app an android app connected to back4app note follow the install parse sdk tutoria l to create an android studio project connected to back4app a device (or virtual device ) running android 4 1 (jelly bean) or newer let’s get started! before next steps, we need to connect back4app back4app to our application you should save the appid appid and clientkey clientkey from the back4app back4app to string xml string xml file and then init parse parse in our app java app java or app kt app kt file follow the new parse app tutorial if you don’t know how to init parse parse to your app or you can download the projects we shared the github links above and edit only the appid appid and clientkey clientkey parts according to you at any time, you can access the complete android project built with this tutorial at our github repositories kotlin example repository java example repository 1 save some data on back4app in this step, we will create a class with the js console and javascript codes provided by parse 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 author , book book , publisher publisher and bookstore bookstore , in which book book has a 1\ n relation with publisher publisher and n\ n with author author , and bookstore bookstore has an n\ n relation with book 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 go ahead and create the classes with the following example content 1 // add objects and create tables 2 // authors 3 const authora = new parse object('author'); 4 authora set('name', 'aaron writer'); 5 await authora save(); 6 7 const authorb = new parse object('author'); 8 authorb set('name', 'beatrice novelist'); 9 await authorb save(); 10 11 const authorc = new parse object('author'); 12 authorc set('name', 'casey columnist'); 13 await authorc save(); 14 15 // publishers 16 const publishera = new parse object('publisher'); 17 publishera set('name', 'acacia publishings'); 18 await publishera save(); 19 20 const publisherb = new parse object('publisher'); 21 publisherb set('name', 'birch distributions'); 22 await publisherb save(); 23 24 // books 25 const booka = new parse object('book'); 26 booka set('title', 'a love story'); 27 booka set('publisher', publishera); 28 booka set('publishingdate', new date('05/07/1998')); 29 const bookarelation = booka relation("authors"); 30 bookarelation add(authora); 31 await booka save(); 32 33 const bookb = new parse object('book'); 34 bookb set('title', 'benevolent elves'); 35 bookb set('publisher', publisherb); 36 bookb set('publishingdate', new date('11/31/2008')); 37 const bookbrelation = bookb relation("authors"); 38 bookbrelation add(authorb); 39 await bookb save(); 40 41 const bookc = new parse object('book'); 42 bookc set('title', 'can you believe it?'); 43 bookc set('publisher', publisherb); 44 bookc set('publishingdate', new date('08/21/2018')); 45 const bookcrelation = bookc relation("authors"); 46 bookcrelation add(authora); 47 bookcrelation add(authorc); 48 await bookc save(); 49 50 // bookstore 51 const bookstorea = new parse object('bookstore'); 52 bookstorea set('name', 'books of love'); 53 const bookstorearelation = bookstorea relation("books"); 54 bookstorearelation add(booka); 55 await bookstorea save(); 56 57 const bookstoreb = new parse object('bookstore'); 58 bookstoreb set('name', 'fantasy books'); 59 const bookstorebrelation = bookstoreb relation("books"); 60 bookstorebrelation add(bookb); 61 await bookstoreb save(); 62 63 const bookstorec = new parse object('bookstore'); 64 bookstorec set('name', 'general books'); 65 const bookstorecrelation = bookstorec relation("books"); 66 bookstorecrelation add(booka); 67 bookstorecrelation add(bookc); 68 await bookstorec save(); 69 70 console log('success'); 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 book results by the publisher, searching for the ones that belong to the publisher publisher “acacia publishings” (or “publisher a”) using the basic parsequery equalto parsequery equalto method 1 progressdialog show(); 2 parsequery\<parseobject> publisherquery = new parsequery<>("publisher"); 3 publisherquery whereequalto("name", "acacia publishings"); 4 try { 5 parseobject publisher = publisherquery getfirst(); 6 parsequery\<parseobject> bookquery = new parsequery\<parseobject>("book"); 7 bookquery whereequalto("publisher", publisher); 8 bookquery findinbackground((objects, e) > { 9 progressdialog hide(); 10 if (e == null) { 11 initdata(objects); 12 } else { 13 toast maketext(this, e getlocalizedmessage(), toast length short) show(); 14 } 15 }); 16 } catch (parseexception e) { 17 progressdialog hide(); 18 e printstacktrace(); 19 }1 progressdialog? show() 2 val publisherquery = parsequery\<parseobject>("publisher") 3 publisherquery whereequalto("name", "acacia publishings") 4 try { 5 val publisher = publisherquery first 6 val bookquery = parsequery\<parseobject>("book") 7 bookquery whereequalto("publisher", publisher) 8 bookquery findinbackground { objects list\<parseobject>?, e parseexception? > 9 progressdialog? hide() 10 if (e == null) { 11 initdata(objects!!) 12 } else { 13 toast maketext(this, e localizedmessage, toast length short) show() 14 } 15 } 16 } catch (e parseexception) { 17 progressdialog? hide() 18 e printstacktrace() 19 } let’s now query which bookstore bookstore objects contain book book objects with publishing date greater than 01/01/2010, using an inner query with the parsequery wheregreaterthan parsequery wheregreaterthan method and then the parsequery wherematchesquery parsequery wherematchesquery method 1 progressdialog show(); 2 parsequery\<parseobject> bookquery = new parsequery<>("book"); 3 4 calendar calendar = calendar getinstance(); 5 calendar set(2010,1,1,59,59,59); 6 date date = calendar gettime(); 7 8 bookquery wheregreaterthan("publishingdate", date); 9 10 parsequery\<parseobject> bookstorequery = new parsequery<>("bookstore"); 11 bookstorequery wherematchesquery("books",bookquery); 12 bookstorequery findinbackground((objects, e) > { 13 progressdialog hide(); 14 if (e==null){ 15 initdata(objects); 16 } else { 17 toast maketext(this, e getlocalizedmessage(), toast length short) show(); 18 } 19 });1 progressdialog? show() 2 val bookquery = parsequery\<parseobject>("book") 3 val calendar = calendar getinstance() 4 calendar\[2010, 1, 1, 59, 59] = 59 5 val date = calendar time 6 bookquery wheregreaterthan("publishingdate", date) 7 val bookstorequery = parsequery\<parseobject>("bookstore") 8 bookstorequery wherematchesquery("books", bookquery) 9 bookstorequery findinbackground { objects list\<parseobject>?, e parseexception? > 10 progressdialog? hide() 11 if (e == null) { 12 initdata(objects!!) 13 } else { 14 toast maketext(this, e localizedmessage, toast length short) show() 15 } 16 } now lets create a more complex relational query, looking for bookstore bookstore objects that have at least one book book written by author author “aaron writer” (or “authora”), using whereequalto whereequalto and wherematchesquery wherematchesquery 1 progressdialog show(); 2 parsequery\<parseobject> authorquery = new parsequery<>("author"); 3 authorquery whereequalto("name","aaron writer"); 4 try { 5 parseobject authora = authorquery getfirst(); 6 parsequery\<parseobject> bookquery = new parsequery<>("book"); 7 bookquery whereequalto("authors",authora); 8 9 parsequery\<parseobject> bookstorequery = new parsequery<>("bookstore"); 10 bookstorequery wherematchesquery("books",bookquery); 11 12 bookstorequery findinbackground((objects, e) > { 13 progressdialog hide(); 14 if (e==null){ 15 initdata(objects); 16 } else { 17 toast maketext(this, e getlocalizedmessage(), toast length short) show(); 18 } 19 }); 20 21 22 } catch (parseexception e) { 23 progressdialog hide(); 24 e printstacktrace(); 25 }1 progressdialog? show() 2 val authorquery = parsequery\<parseobject>("author") 3 authorquery whereequalto("name", "aaron writer") 4 try { 5 val authora = authorquery first 6 val bookquery = parsequery\<parseobject>("book") 7 bookquery whereequalto("authors", authora) 8 val bookstorequery = parsequery\<parseobject>("bookstore") 9 bookstorequery wherematchesquery("books", bookquery) 10 bookstorequery findinbackground { objects list\<parseobject>?, e parseexception? > 11 progressdialog? hide() 12 if (e == null) { 13 initdata(objects!!) 14 } else { 15 toast maketext(this, e localizedmessage, toast length short) show() 16 } 17 } 18 } catch (e parseexception) { 19 progressdialog? hide() 20 e printstacktrace() 21 } 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