1 00:00:00,540 --> 00:00:05,090 To start writing queries, we first need to create the data access objects. 2 00:00:05,090 --> 00:00:08,260 Let's start with creating the Dao for our topping table. 3 00:00:08,260 --> 00:00:12,350 In Android, we create a data access object as an interface, so 4 00:00:12,350 --> 00:00:13,650 inside our data package. 5 00:00:15,686 --> 00:00:19,858 Let's create a new interface called ToppingDao. 6 00:00:24,777 --> 00:00:26,147 Make sure to pick Interface. 7 00:00:28,636 --> 00:00:31,682 Then to designate it as a data access object, 8 00:00:31,682 --> 00:00:34,500 we just need to add the @Dao annotation. 9 00:00:36,450 --> 00:00:38,440 And hit Enter to import it. 10 00:00:38,440 --> 00:00:42,330 Inside the interface, we'll declare each of our queries as a function 11 00:00:42,330 --> 00:00:46,510 with an annotation to tell room how to handle the query. 12 00:00:46,510 --> 00:00:51,500 Let's see how it works by creating a query to select all the toppings. 13 00:00:51,500 --> 00:00:56,266 Let's add a function called getAll, fun getAll and 14 00:00:56,266 --> 00:01:00,069 let's make it return a list of toppings. 15 00:01:04,580 --> 00:01:09,150 Remember since this is an interface, we don't need to add a function body. 16 00:01:09,150 --> 00:01:11,970 Then to tell room how to get the results for 17 00:01:11,970 --> 00:01:15,520 this function, let's add a line above this one. 18 00:01:15,520 --> 00:01:21,290 And add the @Query annotation and some parentheses. 19 00:01:21,290 --> 00:01:25,680 And inside the parentheses, we just need to declare our query as a string. 20 00:01:26,960 --> 00:01:33,440 So let's type select * from topping. 21 00:01:33,440 --> 00:01:34,380 And there we go. 22 00:01:34,380 --> 00:01:38,680 We've got a function that will return all of the toppings and the topping table. 23 00:01:38,680 --> 00:01:42,670 Also notice that we have syntax highlighting in our SQL. 24 00:01:42,670 --> 00:01:43,760 Thanks to room, 25 00:01:43,760 --> 00:01:48,720 Android now has a few checks to make sure queries are correct before we run the app. 26 00:01:49,720 --> 00:01:51,450 It even knows what tables we have. 27 00:01:52,490 --> 00:01:57,320 So if I accidentally change this toppin, we'll get an error. 28 00:01:57,320 --> 00:02:02,090 All right, let's move on to selecting a specific topping by its ID. 29 00:02:02,090 --> 00:02:05,425 Let's add a couple of lines below our getAll function and 30 00:02:05,425 --> 00:02:10,518 then create a new function called getTopping, By Id, 31 00:02:10,518 --> 00:02:16,601 then let's add an ID parameter as in Int, and make sure it returns a topping. 32 00:02:19,906 --> 00:02:22,718 Moving on to the query, let's add the annotation. 33 00:02:25,726 --> 00:02:27,390 And then add the query itself. 34 00:02:28,900 --> 00:02:35,713 Let's go with select * from topping, where id equals, and 35 00:02:35,713 --> 00:02:41,470 this is where we need to use our id parameter. 36 00:02:41,470 --> 00:02:45,090 To reference a parameter in a query, you just prefix it with a colon. 37 00:02:46,110 --> 00:02:48,120 So let's add colon id. 38 00:02:49,360 --> 00:02:53,160 And now we can get a specific topping just from its id. 39 00:02:53,160 --> 00:02:56,950 To finish up our ToppingDao, all we need is an insert statement. 40 00:02:56,950 --> 00:03:00,940 Represented as a function, it would take in a topping and return nothing. 41 00:03:02,130 --> 00:03:04,060 Let's add a couple lines at the bottom and 42 00:03:04,060 --> 00:03:10,040 then declare a new insert function to do just that, fun_insert. 43 00:03:10,040 --> 00:03:10,968 And it will take in a topping. 44 00:03:13,977 --> 00:03:15,370 And return nothing. 45 00:03:15,370 --> 00:03:18,330 Then to tell room how to handle the insert, 46 00:03:18,330 --> 00:03:21,930 there's actually a special @Insert annotation. 47 00:03:21,930 --> 00:03:27,740 So all we need to do is add @Insert, and we're done. 48 00:03:29,000 --> 00:03:31,640 Great work finishing your first data access object. 49 00:03:31,640 --> 00:03:33,440 I know it might feel like a lot, but 50 00:03:33,440 --> 00:03:37,420 one way to make sure you really understand something is to get some practice. 51 00:03:37,420 --> 00:03:40,470 Why don't you try to create the other two dials on your own? 52 00:03:40,470 --> 00:03:43,300 You can find the required queries below, in the teacher's notes. 53 00:03:43,300 --> 00:03:45,160 I'll show you how I do it in the next video.