Firebase Query Android Tutorial - Querying Firebase Realtime Database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi guys this is Bilal Khan and you are watching simplified coding I hope you remember the firebase real-time database series if you don't then go to that series first before watching this video in that series we learn about creating reading updating and deleting data in firebase real-time database as you all know firebase database is a no SQL database so we do not run the queries as we run in the SQL database but it does not mean that we cannot query firebase database so this video is about learning how to query firebase database so I will show you some often used MySQL queries and then we will see the firebase equivalent of those queries so let's start as you can see I have my project ready I have already created the project and this is my database let me show you so this is based database where we have a number of artists every artist has age country john ray ID and name okay so the first thing we will do is we will read all the artists and this thing we have already done in the fire base real time database series okay so guys i have already created the project i have created the model class or Pozzo class I have created the recyclerview adapter this is the main activity so the basic structure of the project is ready if you don't know how to work with recyclerview or how to work with firebase real time database then the link to the tutorial of firebase real-time database and recyclerview is given in the description of the video you should first watch those videos okay so here I have the recyclerview the recyclerview adapter the list of artists and I also have the value event listener to read the artists from the firebase database okay so the first thing we will learn here as the first query so assume if it does a SQL database then to read all the artists assume this as a table named artist then to read all the artists from the table what we write we write select a strike from our tests so this is the SQL query that we will write if we want to read all the artists from our SQL database but right now we have firebase database so in firebase we can just get the reference of this node to read all the nodes inside this node okay and it is very simple I have already done this thing in the previous sorry in the firebase real time database series video okay so let's do it so here we can create a database reference object so we have database reference DB artist okay then here we can write DB our tests equals to firebase database dot ket and stands dot cat reference and we need to get the reference of this our tests know so here we will pass our tests okay that's it now we can simply attach this value event listener to this node or database reference object to read all the values so let's do it so this is for select astrick from our tests or for reading all the values of a particular node let me show you it is working so guys as you can see we have all the artists here so this was the first query now let's move ahead the second query is finding a given our test ok let's say we knew the ID let's say we know this ID and we want to get the artist by this ID so in this case what we do in SQL is let me show you we write select astrick from artist fair ID equals to the given ID or this ID okay so this is what we do in SQL but in case of firebase real-time database what we write as we create a query object okay so we have a query object here okay equals to again we will get the reference of this node okay so I will copy this thing till here so we have firebase database dot cat and stands dot get reference but this time we do not need all the artists okay what we need is we need a particular artist given by an ID okay so first we will order by ID okay we will order all the artists by ID and for this we have a method named order by child okay so here we will write order by child and here we need to specify the name of the child so in this case we want to order the nodes by ID okay ID as you can see we have ID okay so we want to order by ID so for this we will write order by child ID and then here we will attach equal to okay and we have an ID deciding oops so equal to this ID now this query will return the artist whose ID is equal to this given ID okay so let me show you it is working so for now I will comment this line and then I will attach the value event listener to this query okay that's it let me show you it is working as you can see we have the artist whose ID is equal to the given ID you can see we have artifice love so the square is working fine so if you want to run a query like this you have to use this method okay now the next thing that we will see is we have a list all the artists by their country ok so if it were a SQL database what we write we write select astrick from our tests where country equals to let's say India okay so this is what we write an SQL database but for firebase we can again create a query let's say query 3 equals to firebase database dot ket N stands don't get reference again we will get the reference of our test node then again we will order by child but this time we need to order by country okay and then again we will write the equals to and let's say we want to list all the artists from India okay so again I will comment this thing here and let's do it query three dot add listener for single value event and I will pass our value command listener so let me show you it is working you can see we have all the artists from India okay so it is working absolutely fine now the next query now let's say we want to limit the artist to a specified number so in that case we write something like this an SQL select a strike from artists limit let's say - okay now in firebase database we can do something like this copy the same thing till here only copy this again we will get the reference of artists node and then we can simply limit to first okay so if you want the first two records only you can limit to first you can use this method limit to first okay so let's make it query for and again we can attach the value event listener to check it is working okay so let me show you again you can see we have only to records from the top okay first as artificial and then we have our Egyptian so it is working fine now let's move ahead to our next query so the next query is getting all the artists whose age is less than 30 okay and to do something like this in SQL database we write select a strike from artists where age is less than 30 but in firebase real-time database we can again create our reference to this artists node and then we need to order by age okay so we will use order by child and we will pass age then we can use the method and act so we have two methods start at and end at so if you want to use and add to determine or to get all the artists whose age as less than 30 then here we can write 29 and this basically means less than 30 okay so we have query 5 and let me show you again it is working query five dot add listener for single value amend and then we will pass the listener okay let me show you it is working you can see we have all the artist age less than 30 okay so it is working absolutely fine now the last query let's say we need to find all the artists whose name starts with the letter A okay so in this case in SQL we write select a strike from artists their name equals to a and a percentage sign okay but in firebase real time database we write at as query 26 equals to firebase database dot ket n stands dot get reference okay and again we will get the reference of this artists node and then we will order by child and this time we need name okay and then we will use the start at and and add function so we need the artist name starting with a and we also need to add and add and a here as well but for end act we need to specify the Unicode character witchers after every character okay so this is very important so here we need to write backslash uf8 effect if you will not write this it will not work okay so this will return us all the artists name starting with a so let me show you again it is working query sex dot add listener for single value event and then our value event listener so you see it as working fine oops does not working we have a rigid sang artifice LOM and Mohammed alpha and this is because we forget to comment this line okay so we have to comment this tank and here as well okay that's it so we will read only for query six now let us try again you can see we have all the artist name starting with the letter A okay so does working so this is how we query firebase real time database so that's all for this video friends I hope you found this helpful if you are having any question you can leave it on the comments and if you like this video then please hit on the like button subscribe to my channel and share this video with your friends and you can get the source code from the github repository the link is given in the description of the video so thank you guys this is Bilal Khan signing off [Music]
Info
Channel: Simplified Coding
Views: 83,674
Rating: undefined out of 5
Keywords: firebase query android example, firebase realtime database tutorial android, firebase data snapshot android, firebase retrieve data android example, firebase query by key, firebase database android
Id: WeoryL3XyA4
Channel Id: undefined
Length: 15min 47sec (947 seconds)
Published: Tue Apr 17 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.