primpap primpap - 1 year ago 129
Android Question

Debugging sqlite database on the device

I am presently working on an WiFi application for Android. I am having trouble trying to access the database on the device. Debugging in the emulator doesn't work for me, because there is no WiFi support in the emulator. I tried pulling the database file out of the device by using

adb pull data/data/package-name/databases/database-name

But I get the error "Permission denied.".
In this answer Android: Where are database files stored?, Commonsware has suggested to pull database file by running in debug mode. But it doesn't work too. Any help on how to debug the database without rooting the device would be much appreciated.

Answer Source

I'll repeat myself from another answer:

Starting from API level 8 (Android 2.2), if you build the application as debuggable, you can use the shell run-as command to run a command or executable as a specific user/application or just switch to the UID of your application so you can access its data directory.

So if you wish to pull your application database from the device you should run the application in the debug mode (to do so, simply run it from the Eclipse), connect with adb -d shell and run the following commands:

run-as package-name
cat /data/data/package-name/databases/database-name >/sdcard/database-name

This will copy yourdatabase.db to the root of your SD card. Now you can easily get it from there (by using USB storage, adb pull or whatever else you like).
UPDATE @Shiki provided a convenient one-liner one can use without entering the adb shell:

adb -d shell 'run-as com.yourpackage cat /data/data/com.yourpackage/databases/dbname.sqlite > /sdcard/dbname.sqlite'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download