117

I'm working on an Android application that stores data in a SQLite database. My question is, where does this database file get stored on the filesystem when you're using an emulator?

I have seen that it's stored in

/data/data/package_name/databases

but I need to know where on my local machine's hard drive that actually maps to. The database persists on multiple runs of the emulator, even after having shut down the machine, so it can't just reside in RAM...

I82Much
  • 26,901
  • 13
  • 88
  • 119

10 Answers10

123

An update mentioned in the comments below:

You don't need to be on the DDMS perspective anymore, just open the File Explorer from Eclipse Window > Show View > Other... It seems the app doesn't need to be running even, I can browse around in different apps file contents. I'm running ADB version 1.0.29


Or, you can try the old approach:

Open the DDMS perspective on your Eclipse IDE

(Window > Open Perspective > Other > DDMS)

and the most important:

YOUR APPLICATION MUST BE RUNNING SO YOU CAN SEE THE HIERARCHY OF FOLDERS AND FILES.

Then in the File Explorer Tab you will follow the path :

data > data > your-package-name > databases > your-database-file.

Then select the file, click on the disket icon in the right corner of the screen to download the .db file. If you want to upload a database file to the emulator you can click on the phone icon(beside disket icon) and choose the file to upload.

If you want to see the content of the .db file, I advise you to use SQLite Database Browser, which you can download here.

PS: If you want to see the database from a real device, you must root your phone.

rogcg
  • 10,451
  • 20
  • 91
  • 133
  • 5
    If anyone is wondering, you've got to click on the 'File Explorer' tab. – MSpeed Dec 13 '10 at 11:03
  • Hi cyberrog, i found database file but can you tell me how to open this file? or how to view it's contents? – Nilesh Nikumbh Jun 20 '11 at 08:10
  • 4
    @NileshNikumbh Select the file, and click on the disket icon and select where you wanna store it. You also need to download the SQLite Browser. http://sourceforge.net/projects/sqlitebrowser/ After you install it, just open the .db file, chooose the Browse Data tab, and the column you want to see. – rogcg Jun 20 '11 at 12:15
  • Great post! I'm having issues with getting the sqlite file from the emulator device: `Failed to pull selection` `(null)` – Jan-Terje Sørensen Mar 16 '12 at 08:03
  • @OckhamsRazor If you want to see the database from a real device(not emulator) you must root your phone. – rogcg Mar 28 '12 at 13:51
  • Great answer 1 point up. May I ask U a Q, and how can I set the SQLite Database Browser to view a dbs? – Arthur Kushman Jul 11 '12 at 12:27
  • You need not use the DDMS perspective anymore, but may open the File Explorer from Eclipse > Window > Show View > Other... It seems the app doesn't need to be running even, I can browse around in different apps file contents. I'm running ADB version 1.0.29 – Vanja Oct 24 '12 at 10:15
  • +1 for the reference to **root**. On a Jelly Bean device, you cannot even `adb pull` (with the *exact* path!), if you are not **root**. – Bill The Ape Jul 02 '13 at 07:51
  • There is a `data` folder but either it is empty or I have no access to its contents....*I have my phone rooted*..... What say? – Anas Azeem Jan 11 '14 at 08:03
  • @AnasAzeem maybe this can help you.. http://stackoverflow.com/questions/9797019/android-eclipse-ddms-cant-pull-file-from-rooted-device – rogcg Feb 26 '14 at 18:26
  • What about **journal** file? I have a DB and a journal file. DB will work standalone after export? – János Aug 31 '16 at 08:40
58

The filesystem of the emulator doesn't map to a directory on your hard drive. The emulator's disk image is stored as an image file, which you can manage through either Eclipse (look for the G1-looking icon in the toolbar), or through the emulator binary itself (run "emulator -help" for a description of options).

You're best off using adb from the command line to jack into a running emulator. If you can get the specific directory and filename, you can do an "adb pull" to get the database file off of the emulator and onto your regular hard drive.

Edit: Removed suggestion that this works for unrooted devices too - it only works for emulators, and devices where you are operating adb as root.

Eric Mill
  • 3,455
  • 1
  • 25
  • 26
  • 6
    I got the DB off via ddms from Eclipse. Made my changes, then pushed back. Worked like a charm. – I82Much Oct 15 '09 at 17:23
  • @I82Much worked for me as well,change eclipse perspective to ddms and then you can get the data file of there.Thanks. – vanzylv Dec 29 '11 at 09:54
  • 2013 Update: On a Jelly Bean device, you cannot `adb pull` (even with the *exact* path!), if you are not **root**. – Bill The Ape Jul 02 '13 at 07:54
  • That's not an update, basically from the start app private files have been private from adb on secured devices. But this question is about the emulator, where adb runs as root. – Chris Stratton Mar 03 '14 at 12:26
  • 7
    An example for pulling the database from an Emulator to your local machine is **`adb pull /data/data/com.activeandroid.test/databases/Application.db ~/Development/Application.db`** – Joshua Pinter Apr 07 '14 at 15:18
19

The other answers are severely outdated. With Android Studio, this is the way to do it:

  1. Click on Tools > Android > Android Device Monitor

enter image description here

  1. Click on File Explorer

enter image description here

  1. Navigate to your db file and click on the Save button.

enter image description here

Daniel
  • 37
  • 9
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • 1
    "Android Device Monitor was deprecated in Android Studio 3.1 and removed from Android Studio 3.2. The features that you could use through the Android Device Monitor have been replaced by new features..." https://developer.android.com/studio/profile/monitor – Pete Alvin Oct 29 '19 at 16:55
8

In Android Studio 3.4.1, you can use the search feature of Android Studio to find "Device File Explorer" and then go to the /data/data/package_name/database directory of your emulator.

S. Miller
  • 379
  • 3
  • 15
7

I wrote a simple bash script, which pulls database from android device to your computer (Linux, Mac users)

filename:android_db_move.sh usage: android_db_move.sh com.example.app db_name.db

#!/bin/bash

REQUIRED_ARGS=2
ADB_PATH=/Users/Tadas/Library/sdk/platform-tools/adb
PULL_DIR="~/"

if [ $# -ne $REQUIRED_ARGS ]
    then
        echo ""
        echo "Usage:"
        echo "android_db_move.sh [package_name] [db_name]"
        echo "eg. android_db_move.sh lt.appcamp.impuls impuls.db"
        echo ""
    exit 1
fi;


echo""

cmd1="$ADB_PATH -d shell 'run-as $1 cat /data/data/$1/databases/$2 > /sdcard/$2' "
cmd2="$ADB_PATH pull /sdcard/$2 $PULL_DIR"

echo $cmd1
eval $cmd1
if [ $? -eq 0 ]
    then
    echo ".........OK"
fi;

echo $cmd2
eval $cmd2

if [ $? -eq 0 ]
    then
    echo ".........OK"
fi;

exit 0
Tadas Valaitis
  • 870
  • 10
  • 11
6

Since the question is not restricted to Android Studio, So I am giving the path for Visual Studio 2015 (worked for Xamarin).

Tools-Android-Android Device Monitor

enter image description here

  • Locate the database file mentioned in above image, and click on Pull button as it shown in image 2.
  • Save the file in your desired location.
  • You can open that file using SQLite Studio or DB Browser for SQLite.

Special Thanks to other answerers of this question.

Keerthi
  • 525
  • 8
  • 14
Foyzul Karim
  • 4,252
  • 5
  • 47
  • 70
4

The databases are stored as SQLite files in /data/data/PACKAGE/databases/DATABASEFILE where:

You can see (copy from/to filesystem) the database file in the emulator selecting DDMS perspective, in the File Explorer tab.

caligari
  • 2,110
  • 20
  • 25
3

Simple Solution - works for both Emulator & Connected Devices

1 See the list of devices/emulators currently available.

$ adb devices

List of devices attached

G7NZCJ015313309 device emulator-5554 device

9885b6454e46383744 device

2 Run backup on your device/emulator

$ adb -s emulator-5554 backup -f ~/Desktop/data.ab -noapk com.your_app_package.app;

3 Extract data.ab

$ dd if=data.ab bs=1 skip=24 | openssl zlib -d | tar -xvf -;

You will find the database in /db folder

Mick
  • 30,759
  • 16
  • 111
  • 130
  • If you're on OS X you will likely be missing the zlib part of openssl. You can use this work-around: `$ dd if=data.ab bs=1 skip=24 | python -c "import zlib,sys;sys.stdout.write(zlib.decompress(sys.stdin.read()))" | tar -xvf -` – Lasse Magnussen Jun 03 '17 at 21:44
  • Does not work for me. I'm getting `bash: adb: command not found` – Matthias Feb 10 '20 at 21:03
  • it gives me error that is adb: unable to open file ~/Desktop/data.ab – obaid Nov 04 '20 at 12:52
  • this works for me: (just change the name instead of backup.ab your file name) ( printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" ; tail -c +25 backup.ab ) | tar xfvz - – Shajko Jan 20 '23 at 22:00
2

according to Android docs, Monitor was deprecated in Android Studio 3.1 and removed from Android Studio 3.2. To access files, there is a tab in android studio called "Device File Explorer" bottom-right side of developing window which you can access your emulator file system. Just follow

/data/data/package_name/databases

good luck.

Android device File explorer

vahid sabet
  • 485
  • 1
  • 6
  • 16
1

For Android Studio 3.5, fount it using instructions here: https://developer.android.com/studio/debug/device-file-explorer (View -> Tool Windows -> Device File Explorer -> -> databases

Ana M
  • 467
  • 5
  • 13