Getting a backup of your Android application’s database can be as easy as issuing a command for a virtual device:
adb pull /data/data/your.package.name/databases/database_name.db
On a physical device it gets a bit harder. First you need to enable USB debugging for it. Starting with Android 4.2 you actually have to enable the developer options first by tapping the build number 7 times. Then those options will allow you to enable USB debugging.
For security reasons
adb pull doesn’t work on non-rooted devices. Dumping the database with SQLite3 doesn’t work for the same reasons. And SQLite3.exe won’t be installed on your device anyway as it’s only the command line client and applications don’t need it to access SQLite databases.
Another option is to copy the database to the device’s SD card and access it from there:
adb -d shell run-as your.package.name cat /data/data/your.package.name/databases/database_name.db > /sdcard/database_name.db
This requires the application to be built as debuggable and obviously your device must support SD cards. If your device’s mastermind says “Everybody likes the idea of having an SD card, but in reality it’s just confusing for users” your last hope is having at least Android 4.0. This version introduced the
adb backup command:
adb backup -f /data.ab -noapk your.package.name Now unlock your device and confirm the backup operation.
Your device will show an appropriate confirmation dialog with an option to encrypt your backup if you enter a password. To keep it simple don’t do that and press “Back up my data”. This will create the file “c:\users\account_name\data.ab”.
A Linux system could extract the file’s content with a single command line command. On Windows systems you have to download “Android Backup Extractor”. From it’s .zip file copy “abe.jar” to “c:\users\account_name\”. Then run this from the command line and ignore the warning about “You are not allowed to use strong AES encryption”:
java -jar abe.jar unpack data.ab data.tar
You can open the resulting file “c:\users\account_name\data.tar” for example with 7zip and extract “\apps\your.package.name\db\database_name.db”.
Finally you can access your database backup:
"C:\Program Files (x86)\Android\android-sdk\tools\sqlite3.exe" database_name.db
Some additional info from doing this again after a few months:
- You’ll find adb.exe (Android Debug Bridge) in “C:\Program Files (x86)\Android\android-sdk\platform-tools”. So the backup command becomes
"C:\Program Files (x86)\Android\android-sdk\platform-tools\adb" backup -f /data.ab -noapk your.package.name
- If you mistype your.package.name you’ll still see the success message but nothing will actually happen.
- The command line program (cmd.exe) in Windows shows the current directory in it’s prompt. That is usually “C:\Users\account_name>” and will be the directory your data.ab file gets copied to.
- Hmm… data.ab landed in “C:\Users\account_name\AppData\Local\VirtualStore” this time. Probably because I didn’t run cmd.exe as an administrator now. But doing so leads to a command prompt of “C:\Windows\system32>” so I’m unsure how data.ab ended up in “C:\Users\account_name>” in the previous round.
- Some recent Android update added an additional confirmation screen before starting the backup asking whether you want to allow access from the specified computer.
- You prefer a GUI for manipulating your database once you got the backup? Use SQLite Manager, an add-on for FireFox. Install it, restart FireFox and find it’s menu item in “FireFox” > “Web developers”.
Adding some code to copy the database file may be an easier solution and it works without an SD card. I added a link to a recent post and one to a standalone browser for SQLite databases.
- Android Backup Extractor
- Access Android app data without root – found that when researching for this post after figuring out everything myself 🙂
- SQLite Manager for FireFox
- DB Browser for SQLite
- DbTradeAlert for Android: Add Backup