SQLite is the default database used for local storage across most mobile operating systems. Many cross-platform frameworks offer first class support for SQLite too, including the Ionic Framework and Azure Mobile App Service. I often find it useful to get access to the SQLite database to aid development, testing and debugging. Mostly for my own future reference, this is a quick guide to pull an SQLite database from an Android device and then open it using SQLite Database Browser. While these steps are written for Windows they are mostly the same from macOS.
You will need the Android SDK. If you don’t have it installed, the easiest way to get it is to install Android Studio, which comes bundled with the required SDK.
Connect your device
- Enable developer options.
- Enable USB debugging.
- Connect your device via USB.
- You will be prompted to ‘Allow USB Debugging’. Select OK to allow this.
Get the database
Android Debug Bridge (adb) provides access to a Unix shell that you can use to run a variety of commands on a device. From your command prompt open adb’s shell by running adb shell
. Using the shell you can copy the database out of the application’s data directory to an accessible location on the device.
shell@device:/ $ mkdir /sdcard/data/
shell@device:/ $ run-as <com.your.package>
shell@device:/data/data/com.your.package $ cp databases/<database>.db /sdcard/data/
shell@device:/data/data/com.your.package $ exit
shell@device:/ $ exit
Note: The package name must reference a debuggable package.
Now that the database file is accessible from “/sdcard/data/” you can use adb pull
to pull the file to your local host:
adb pull /sdcard/data/<database>.db .
It is worth mentioning that you can use adb push
to push a file to the device and therefore update the application database. Just follow the steps in reverse.
If you want, you can remove the copy you created in the previous steps. Again, after running adb shell
.
shell@device:/ $ rm -r /sdcard/data/
shell@device:/ $ exit
Open the database
Now you have a copy of the application’s database you’ll need an SQLite compatible database explorer. My go-to tool for browsing and editing SQLite database files is SQLite Database Browser. It’s available for Windows, macOS and Linux and it’s open source too! Once installed, just select Open Database.