1 00:00:03,760 --> 00:00:09,140 G'day everyone, welcome back. Before we continue with upgrading our database, 2 00:00:09,140 --> 00:00:14,410 there's a correction we need to make. Back in section 12, there was a typo in 3 00:00:14,410 --> 00:00:20,840 the video, when we created the AppDatabase class. Let's have a look in there. 4 00:00:20,840 --> 00:00:26,100 On line 20, I set the database version to 3. This is the first version 5 00:00:26,110 --> 00:00:32,738 of our database, and that really should be version 1. Now, just like the apps' version 6 00:00:32,738 --> 00:00:36,010 number, this version can't go backwards. If we 7 00:00:36,010 --> 00:00:41,620 release this app, we'd be stuck with the first database version being 3. That's 8 00:00:41,620 --> 00:00:45,940 not the end of the world - we're about to look at upgrading databases, or you 9 00:00:45,940 --> 00:00:50,950 could just upgrade version 4. But as we've just uninstalled the app from our 10 00:00:50,950 --> 00:00:56,050 emulators, and have lost all the data anyway, it's a good opportunity to 11 00:00:56,050 --> 00:01:04,750 correct that typo and start with version 1 of the database. Change the version on 12 00:01:04,750 --> 00:01:10,900 line 20, to 1. If you watched that video after we added the correction banner, 13 00:01:10,900 --> 00:01:15,609 you'll have already set it to 1. If you watched it before the correction, you'll 14 00:01:15,609 --> 00:01:20,530 need to uninstall the app before running it again. That will delete the old 15 00:01:20,530 --> 00:01:27,009 database and create a new one. If you don't uninstall the app, it'll crash. You 16 00:01:27,009 --> 00:01:31,659 can't use an earlier database version, if the database already exists on the 17 00:01:31,659 --> 00:01:37,600 device. That's easy to check. I'll run the app in my emulator, and it will crash 18 00:01:37,600 --> 00:01:45,149 with a SQLite exception: can't downgrade database from version 3 to 1. 19 00:01:45,630 --> 00:01:50,380 That's not good. Obviously, if we'd released the app, then 20 00:01:50,380 --> 00:01:55,209 we couldn't reduce the database number. Alright, I'll uninstall the app from my 21 00:01:55,209 --> 00:01:59,039 emulator and run it again. 22 00:02:13,880 --> 00:02:18,260 This time it works because the old database file was deleted when I 23 00:02:18,260 --> 00:02:23,270 uninstalled. This is a good time to enter some tasks. You want to make sure that 24 00:02:23,270 --> 00:02:28,160 the upgrade process doesn't delete our data, and we can't really do that if 25 00:02:28,160 --> 00:02:33,290 there are no tasks in the database. I'll create six tasks and I'll speed up this 26 00:02:33,290 --> 00:02:40,090 bit of the video. Watching me type tasks into the app isn't very exciting. 27 00:02:46,150 --> 00:02:50,500 There's been a useful change to Android Studio since I recorded the earlier 28 00:02:50,500 --> 00:02:55,690 sections. It now has a Device File Explorer, that lets you copy your 29 00:02:55,690 --> 00:03:01,000 database off the device. You can start the File Explorer from the View Tool 30 00:03:01,000 --> 00:03:14,739 Windows menu. The tab appears at the bottom right, by default. If you've got 31 00:03:14,739 --> 00:03:19,180 several devices connected, use the drop-down at the top to select the 32 00:03:19,180 --> 00:03:32,010 correct one. Our database is in data/data/learnprogramming.academy.tasktimer in the database's folder. 33 00:03:39,629 --> 00:03:50,280 From there you can right-click Task Timer db and choose Save As. That seems 34 00:03:50,280 --> 00:03:55,720 easy, but it won't work. SQLite often creates additional files. 35 00:03:55,720 --> 00:04:00,299 I've got TaskTimer.db-journal and 36 00:04:00,299 --> 00:04:05,189 you need to transfer both files to your computer, if you want to open the 37 00:04:05,189 --> 00:04:16,700 database. On some versions of Android you'll see a dot db - shm and a dot db - wal file. 38 00:04:16,700 --> 00:04:20,540 It depends on the version of Android that you're running. Right click 39 00:04:20,540 --> 00:04:31,960 on the database's folder, and choose Save As from there. That will copy all the files. 40 00:04:31,960 --> 00:04:37,740 On Windows, Android Studio creates a directory in your Documents folder. 41 00:04:37,740 --> 00:04:49,740 The full path will be something like Documents\AndroidStudio\Device Explorer\emulator name>\data\data 42 00:04:49,740 --> 00:04:55,920 \learnprogramming.academy.tasktimer\ databases 43 00:04:55,920 --> 00:05:01,820 The path that goes after the emulator name reflects the path on the device. I want to keep each version 44 00:05:01,830 --> 00:05:07,320 in my database, so I'll save it to my desktop in a directory called TaskTimer 45 00:05:07,320 --> 00:05:11,419 db - version 1. 46 00:05:26,860 --> 00:05:32,530 That's easier than using the terminal and copying via adb. Note that you don't 47 00:05:32,530 --> 00:05:38,400 have unrestricted access to the files on the device. We're debugging TaskTimer, 48 00:05:38,400 --> 00:05:42,780 which is why the Device Explorer can access its files. You can probably see 49 00:05:42,790 --> 00:05:47,050 files for some other apps, that you've run on the emulator, but a lot of 50 00:05:47,050 --> 00:05:51,920 directories won't show any files. Google have documented it at 51 00:05:51,920 --> 00:06:01,720 https://developer.android.com/studio/debug/device-file-explorer 52 00:06:01,720 --> 00:06:03,480 The note at the top mentions 53 00:06:03,490 --> 00:06:08,140 restrictions on viewing files, and the emulators that give you root access to 54 00:06:08,140 --> 00:06:15,780 the device. There's another note about that, right at the bottom of the page. 55 00:06:15,780 --> 00:06:20,520 One thing we can check is our shared preferences. I'll switch back to the 56 00:06:20,520 --> 00:06:35,300 emulator and tap Settings. Make a change to the settings, then tap OK to save the settings. 57 00:06:35,300 --> 00:06:40,860 Back in the Device File Explorer, right-click the 58 00:06:40,860 --> 00:06:51,400 learnprogramming.academy.tasktimer directory, and select synchronize. We can 59 00:06:51,400 --> 00:06:59,020 now see the shared prefs directory, and I can double click the XML file to open in 60 00:06:59,020 --> 00:07:04,060 Android Studio. That can be handy to check the preferences if you get strange 61 00:07:04,060 --> 00:07:10,210 results. Okay, I'll stop this video here. In the next video, we'll upgrade the 62 00:07:10,210 --> 00:07:13,830 database. See you there.