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 \<font color="#2166ae">compilesdk = 30\</font> , \<font color="#2166ae">minsdk = 23\</font> and \<font color="#2166ae">targetsdk = 30\</font> at any time, you can access the complete android project built with this tutorial at our github repositories kotlin example repository https //github com/templates back4app/android parse sdk kotlin java example repository https //github com/templates back4app/android parse sdk java goal our goal is query data stored on \<font color="#2166ae">back4app\</font> from an \<font color="#2166ae">android\</font> app here is a preview of what we are gonna achieve prerequisites to complete this tutorial, we need android studio https //developer android com/studio/index html an app created on back4app note follow the new parse app tutorial https //www back4app com/docs/get started/new parse app to learn how to create a parse app on back4app an android app connected to back4app note follow the install parse sdk tutoria https //www back4app com/docs/android/parse android sdk l to create an android studio project connected to back4app a device (or virtual device https //developer android com/studio/run/managing avds html ) running android 4 1 (jelly bean) or newer let’s get started! before next steps, we need to connect \<font color="#2166ae">back4app\</font> to our application you should save the \<font color="#2166ae">appid\</font> and \<font color="#2166ae">clientkey\</font> from the \<font color="#2166ae">back4app\</font> to \<font color="#2166ae">string xml\</font> file and then init \<font color="#2166ae">parse\</font> in our \<font color="#2166ae">app java\</font> or \<font color="#2166ae">app kt\</font> file follow the new parse app tutorial https //www back4app com/docs/android/parse android sdk if you don’t know how to init \<font color="#2166ae">parse\</font> to your app or you can download the projects we shared the github links above and edit only the \<font color="#2166ae">appid\</font> and \<font color="#2166ae">clientkey\</font> 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 https //github com/templates back4app/android parse sdk kotlin java example repository https //github com/templates back4app/android parse sdk java 1 save some data on back4app in this step, we will create a class with the js console and javascript codes provided by \<font color="#2166ae">parse\</font> 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 \<font color="#2166ae">author\</font> , \<font color="#2166ae">book\</font> , \<font color="#2166ae">publisher\</font> and \<font color="#2166ae">bookstore\</font> , in which \<font color="#2166ae">book\</font> has a 1\ n relation with \<font color="#2166ae">publisher\</font> and n\ n with \<font color="#2166ae">author\</font> , and \<font color="#2166ae">bookstore\</font> has an n\ n relation with \<font color="#2166ae">book\</font> 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 \<font color="#2166ae">book\</font> results by the publisher, searching for the ones that belong to the \<font color="#2166ae">publisher\</font> “acacia publishings” (or “publisher a”) using the basic \<font color="#2166ae">parsequery equalto\</font> 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 \<font color="#2166ae">bookstore\</font> objects contain \<font color="#2166ae">book\</font> objects with publishing date greater than 01/01/2010, using an inner query with the \<font color="#2166ae">parsequery wheregreaterthan\</font> method and then the \<font color="#2166ae">parsequery wherematchesquery\</font> 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 \<font color="#2166ae">bookstore\</font> objects that have at least one \<font color="#2166ae">book\</font> written by \<font color="#2166ae">author\</font> “aaron writer” (or “authora”), using \<font color="#2166ae">whereequalto\</font> and \<font color="#2166ae">wherematchesquery\</font> 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