Getting Access to the Database4:38 with Ben Deitch
In this video we'll see how to get access to the database!
[MUSIC] 0:00 We've just finishing building our database. 0:00 Now, to finish up the app, we just need to connect it to the UI. 0:07 We'll start by updating the save button and creator activity. 0:10 And by the end, we'll have the UI updating itself whenever anything changes. 0:14 The first step to using our database is to retrieve an instance of it. 0:21 Rather than retrieving our database all over the place, 0:25 let's just get it done once in our application's onCreate method, and 0:28 then make it available to the rest of our project 0:32 by declaring that variable outside of the class to scope it to the entire project. 0:34 Let's go ahead and close the files we have open. 0:40 And then head over to the app file. 0:44 And below our toppings array let's create a new lateinit var 0:48 named db and give it a type of pizza database. 0:53 Then down in onCreate let's add a line at the top and 0:59 set db equal to a new instance of our pizza database. 1:04 So Room.databaseBuilder, 1:09 we'll pass in the context, which is just applicationContext. 1:14 Then the database class PizzaDatabaseclass.java. 1:19 The name of the database which is 1:24 "PizzaDatabase").build(). 1:28 Great. 1:34 Now whenever we want to access our database we'll just type, db, 1:35 and be done with it. 1:39 Now another thing we'll want to do in this onCreate method 1:41 is add our toppings to the database. 1:44 Since we've already got a loop going over the toppings list, 1:46 let's just insert each of these toppings to the database. 1:51 So let's just add db.toppingDao.insert. 1:57 And pass in the topping. 2:03 Then, just to double check things, let's try running the app. 2:06 Don't forget to select app from the drop down at the top before running it. 2:09 Once it runs, it looks like we got an error. 2:18 And if we look in the logcat, And 2:22 filter by errors, We can find the error. 2:28 Cannot access database on the main thread since it may potentially lock the UI for 2:36 a long period of time. 2:41 To fix this, every time we access our database we need to do it on a new thread. 2:43 Let's add a line above our foreach loop and then wrap it inside a thread block. 2:50 If you've not used Kotlin before this is just a nice shortcut to let us 3:01 quickly create new threads. 3:06 Now let's try running the app again. 3:09 Okay? 3:16 Looks like we're getting a UNIQUE constraint failed on our Topping.id. 3:23 And if we look back at the app class we can see that each time we run the app 3:29 we're inserting the same toppings each time. 3:34 Or rather we're trying to insert them. 3:38 But it's not letting us because these toppings are already in the database. 3:41 To fix this let's head over to topping dow. 3:45 And after the Insert annotation, let's add () and 3:54 then specify an on conflict strategy. 3:58 onConflict = and let's set it = 4:01 OnConflictStrategy.replace. 4:05 Now if we try to insert the same topping twice instead of throwing an error 4:11 it will just replace that row in the database. 4:17 Perfect. 4:19 Let's try running the app again. 4:20 And there we go. 4:29 The app ran successfully. 4:30 Now that we've got our database ready with our toppings, 4:32 we can handle saving pizzas in the next video. 4:35
You need to sign up for Treehouse in order to download course files.Sign up