# android adb, retrieve database using run-as

One a non-rooted android device, I can navigate to the data folder containing the database using the run-as command with my package name. Most files types I am content with just viewing, but with the database I would like to pull if from the android device.

Is there a download copy or move command from this part of adb shell? I would like to download the database file and view its content using a database browser.

One answer here involves turning entire application package into a compressed archive, but there is no further answer on how to extract that archive once this is done and moved to the machine, leaving me very sidetracked when there might be a more direct solution to begin with

thank you

-

adb shell "run-as package.name chmod 666 /data/data/package.name/databases/file"
adb shell "run-as package.name chmod 600 /data/data/package.name/databases/file"

UPDATE

the command for Android 5.0+ to save /data/data/package.name/databases/file would be:

adb exec-out run-as package.name cat databases/file > file
-
Or, just use run-as to copy the file to a spot on external storage, and pull the database file from there, rather than messing around with file permissions. –  CommonsWare  Jul 4 '14 at 11:02
The run-as has no permission to write to the sdcard - there is something new added with the latest update... :( –  slott  Jan 13 '15 at 11:09
When I run adb shell "run-as package.name chmod 666 /data/data/package.name/databases/file" adb ends me error -->run-as: exec failed for chmod666 Error:Permission denied . I'm in Nexus 7 without root –  Shudy Jan 13 '15 at 12:02
Write access to external sd card is no longer possible with adb. –  slott  Jan 13 '15 at 13:40
When transferring my database using "adb exec-out run-as package.name cat databases/file > file", I'm getting extra line-breaks (CR LF), making the file unreadable by sqliteman. Looks like the file is being copied as text rather than binary. Is there any way to fix it? –  Gyum Fox  Sep 23 '15 at 13:36

The accepted answer doesn't work anymore for me (blocked by Android?)

> adb shell
shell $run-as com.example.package shell$ chmod 666 databases/file
shell $exit ## exit out of 'run-as' shell$ cp /data/data/package.name/databases/file /sdcard/
shell $run-as com.example.package shell$ chmod 600 databases/file
> adb pull /sdcard/file .
-
This should be the new correct answer OR @Alex P. you should add this answer for newer Android versions. Also in my case I had to use 777 and apply it to the database-directory first/as-well before I could access the db file. –  muetzenflo  Mar 31 '15 at 9:10
My device is rooted, but I found I still need to run su to get the run-as command working. –  ThomasW Apr 9 '15 at 8:26
Hero!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! –  Bear  Aug 10 '15 at 13:30
Thanks working on 5.1! –  Muhammad Babar  Aug 14 '15 at 6:31
What if you don't have SD card? –  Gyum Fox  Sep 23 '15 at 12:19

I've published a simple shell script for dumping databases:

It performs two distinct methods described here:

1. First, it tries to make the file accessible for other users, and attempting to pull it from the device.
2. If that fails, it streams the contents of the file over the terminal to the local machine. It performs an additional trick to remove \r characters that some devices output to the shell.

From here you can use a variety of CLI or GUI SQLite applications, such as sqlite3 or sqlitebrowser, to browse the contents of the database.

-

Here's a solution that works on a device running Android 5.1. The following example is for Windows.

You need sed ([https://www.gnu.org/software/sed/manual/sed.html] or sed.exe on windows, e.g. from cygwin.) ( On Unix, it'll just be there ;) ). To remove bad '\r' characters, at least on windows.

Now just run the following command:

adb exec-out "run-as com.yourcompany.yourapp /data/data/com.yourcompany.yourapp/databases/YourDatabaseName" | c:\cygwin\bin\sed.exe 's/\x0D\x0A/\x0A/'>YourDatabaseName.db

The sed command strips out trailing /r characters.

Of course you should replace "com.yourcompany.yourapp" with the package name of the app and "YourDatabaseName" with the name of the database in the app.

-

For app's debug version, it's very convenient to use command adb exec-out run-as xxx.yyy.zzz cat somefile > somefile to extract a single file. But you have to do multiple times for multiple files. Here is a simple script I use to extract the directory.

#!/bin/bash
P=
F=
D=

function usage()
{
echo "$(basename$0) [-f file] [-d directory] -p package"
exit 1
}

while getopts ":p:f:d:" opt
do
case $opt in p) P=$OPTARG
echo package is $OPTARG ;; f) F=$OPTARG
echo file is $OPTARG ;; d) D=$OPTARG
echo directory is $OPTARG ;; \?) echo Unknown option -$OPTARG
usage
;;
\:)
echo Required argument not found -$OPTARG usage ;; esac done [ x$P == x ] && {
echo "package can not be empty"
usage
exit 1
}

[[ x$F == x && x$D == x ]] && {
echo "file or directory can not be empty"
usage
exit 1
}

function file_type()
{
# use printf to avoid carriage return
__t=$(adb shell run-as$P "sh -c \"[ -f $1 ] && printf f || printf d\"") echo$__t
}

function list_and_pull()
{
t=$(file_type$1)
if [ $t == d ]; then for f in$(adb shell run-as $P ls$1)
do
# the carriage return output from adb shell should
# be removed
mkdir -p $(echo -e$1 |sed $'s/\r//') list_and_pull$(echo -e $1/$f |sed $'s/\r//') done else echo pull file$1
[ ! -e $(dirname$1) ] && mkdir -p $(dirname$1)
$(adb exec-out run-as$P cat $1 >$1)
fi
}

[ ! -z $D ] && list_and_pull$D
[ ! -z $F ] && list_and_pull$F

Hope it would be helpful. This script is also available at gist.

Typical usage is

\$ ./exec_out.sh -p com.example.myapplication -d databases

then it will extract all files under your apps databases directory, which is /data/data/com.example.myapplication/databases, into current directory.

-

why dont you just view the database within the adb shell ?

in adb on most phones or on emulator call sqlite3

now run standard sql commands right on the prompt.

-
I like graphical representations of the table data, which requires me to load a file in. Maybe someone could script an sqlite browser to connect to android devices via adb though –  CQM  Mar 3 '14 at 17:47
sqlite3 is not available on a regular device - only emulators and dev devices. –  slott  Jan 13 '15 at 13:40

05-18 1421
06-04 1万+
12-21 1737
10-12 3954
03-07 5152