G'day everyone, welcome back. Before we continue with upgrading our database,
there's a correction we need to make. Back in section 12, there was a typo in
the video, when we created the AppDatabase class. Let's have a look in there.
On line 20, I set the database version to 3. This is the first version
of our database, and that really should be version 1. Now, just like the apps' version
number, this version can't go backwards. If we
release this app, we'd be stuck with the first database version being 3. That's
not the end of the world - we're about to look at upgrading databases, or you
could just upgrade version 4. But as we've just uninstalled the app from our
emulators, and have lost all the data anyway, it's a good opportunity to
correct that typo and start with version 1 of the database. Change the version on
line 20, to 1. If you watched that video after we added the correction banner,
you'll have already set it to 1. If you watched it before the correction, you'll
need to uninstall the app before running it again. That will delete the old
database and create a new one. If you don't uninstall the app, it'll crash. You
can't use an earlier database version, if the database already exists on the
device. That's easy to check. I'll run the app in my emulator, and it will crash
with a SQLite exception: can't downgrade database from version 3 to 1.
That's not good. Obviously, if we'd released the app, then
we couldn't reduce the database number. Alright, I'll uninstall the app from my
emulator and run it again.
This time it works because the old database file was deleted when I
uninstalled. This is a good time to enter some tasks. You want to make sure that
the upgrade process doesn't delete our data, and we can't really do that if
there are no tasks in the database. I'll create six tasks and I'll speed up this
bit of the video. Watching me type tasks into the app isn't very exciting.
There's been a useful change to Android Studio since I recorded the earlier
sections. It now has a Device File Explorer, that lets you copy your
database off the device. You can start the File Explorer from the View Tool
Windows menu. The tab appears at the bottom right, by default. If you've got
several devices connected, use the drop-down at the top to select the
correct one. Our database is in data/data/learnprogramming.academy.tasktimer in the database's folder.
From there you can right-click Task Timer db and choose Save As. That seems
easy, but it won't work. SQLite often creates additional files.
I've got TaskTimer.db-journal and
you need to transfer both files to your computer, if you want to open the
database. On some versions of Android you'll see a dot db - shm and a dot db - wal file.
It depends on the version of Android that you're running. Right click
on the database's folder, and choose Save As from there. That will copy all the files.
On Windows, Android Studio creates a directory in your Documents folder.
The full path will be something like Documents\AndroidStudio\Device Explorer\emulator name>\data\data
\learnprogramming.academy.tasktimer\ databases
The path that goes after the emulator name reflects the path on the device. I want to keep each version
in my database, so I'll save it to my desktop in a directory called TaskTimer
db - version 1.
That's easier than using the terminal and copying via adb. Note that you don't
have unrestricted access to the files on the device. We're debugging TaskTimer,
which is why the Device Explorer can access its files. You can probably see
files for some other apps, that you've run on the emulator, but a lot of
directories won't show any files. Google have documented it at
https://developer.android.com/studio/debug/device-file-explorer
The note at the top mentions
restrictions on viewing files, and the emulators that give you root access to
the device. There's another note about that, right at the bottom of the page.
One thing we can check is our shared preferences. I'll switch back to the
emulator and tap Settings. Make a change to the settings, then tap OK to save the settings.
Back in the Device File Explorer, right-click the
learnprogramming.academy.tasktimer directory, and select synchronize. We can
now see the shared prefs directory, and I can double click the XML file to open in
Android Studio. That can be handy to check the preferences if you get strange
results. Okay, I'll stop this video here. In the next video, we'll upgrade the
database. See you there.