Where does Android emulator store SQLite database? - android

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...

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.

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.

The other answers are severely outdated. With Android Studio, this is the way to do it:
Click on Tools > Android > Android Device Monitor
Click on File Explorer
Navigate to your db file and click on the Save button.

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.

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

Since the question is not restricted to Android Studio, So I am giving the path for Visual Studio 2015 (worked for Xamarin).
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.

The databases are stored as SQLite files in /data/data/PACKAGE/databases/DATABASEFILE where:
PACKAGE is the package declared in the AndroidManifest.xml (tag "manifest", attribute "package")
DATABASEFILE is the name passed when you call the SQLiteOpenHelper constructor as explained here: http://developer.android.com/guide/topics/data/data-storage.html#db
You can see (copy from/to filesystem) the database file in the emulator selecting DDMS perspective, in the File Explorer tab.

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

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.

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

Related

Android Studio - Android Device Monitor's File explorer not working

I have just learnt using preferences in Android. For debugging purpose I need to access the preferences files. I googled and found the default location of the preference file and also how to access it via Android Device Monitor.
Somehow I am not able to open any folders under the File explorer tab of the Android device monitor. They just aren't responding. I tried waiting for ADM to load and tried again but no success. (Double click on folder is not working, STRANGE!)
Do we need to configure before using it? I am using Android studio.
Any help would be appreciated.
Attaching image
Finally I found how to make it work.
goto run -> location where your adb is
Ex: C:\Program Files (x86)\Android\android-sdk\platform-tools
type adb root , Enter (Your phone must have root)
If this doesn't work, install this app on your phone: [root] adbd Insecure or its free alternative here (update: link broken now).
Get more info at http://forum.xda-developers.com/showthread.php?t=1687590
You will need either to use the emulator or to have a rooted phone to use all functionalities.
Root your device and open adb shell and change the permissions as:
$ adb shell
$ su
1|root#android:/ # chmod -R 777 data/

how to see sqlite db with out android device moniter?

my android device monitor is not working in android studio, is any other way to see existing sq-lite database,and give the possible reason of my android device monitor issue?.
Its nice question for newbie in android.
For seeing SQLite database you have to follow below step.
Step 1: Setup emulator and run it.
If you have to see SQLite database you must Run your application in
Android Emulator.
(Note:- If you run your application in physical device then you are not able to see database).
Step 2: Now Run you Android Device Monitor from Android studio,now find your android emulator there,click on it and you will show File Explorer in this monitor.
Step 3: In the File Explorer there are Data Folder.Go to Data folder,there is another data folder in it. Now Find your Application here.in your application folder there is folder with name database,Which contain SQLite database file.
(Note: File Explorer>data>data>Your Application Package>database)
Step 4: There are option in the top,From there you can "Pull a file from Device(emulator)",Click on it and save it to your computer.
So here is the file by reading it you can See your database. so,now how can we read it?
Step 5: For reading the file download SQLite Browser from here install it and open it in your computer.
Step 6: In this Browser Click on Open Database and open the file which you have pull from Device(emulator).
So here you can see your database structure and also browse data.
Even, You can update data and Push the file with the Android Device monitor for manually update data.
You can extract your device database in linux (ubuntu) system from the following commands
Note: Make sure your device is connected to adb and showing in adb devices list
adb backup -f ~/data.ab -noapk <your app package name ex app.package.name>
dd if=data.ab bs=1 skip=24 | openssl zlib -d | tar -xvf -
The first command will create a data.ab file in home directory and second command should be run from the home directory as data.ab file is in that directory after running second command there will be a app folder created in your home directory, you can see your app package name directory under it and file your db file and shared preference xml.
You can use navicate premium to create explore and query to sqlite db.
In go to file explore of your emulator go to data > data > your app and database folder extract it to your computer and open in navicate.

Pull database from Genymotion emulator

I am using Windows7 and want to extract/pull the application database to the local windows machine's folder.
I have searched through internet and found the adb command as below :
adb shell su 0 cp /data/data/<package.name>/databases/<database.name> /mnt/shared/
I have tried the same by creating shared folder for the Virtual box.
But getting error is like : read-only file system
I have also check that read-only is disabled.
Please I need help.
Screenshots :
I'm not sure for Windows but on Mac you just call
adb pull /data/data/<package.name>/database/<database.name> <local.file.path>
As your phone under Genymotion is rooted, you can go in the DDMS of Eclipse.
Then, Click on the tab file explorer.
Then go to folder /data/data/<your.package.name>/ then pull the file from the path in which you saved your database.

Android studio file explorer not showing files in data folder

I am using android studio and 'samsung tab 2' as my testing device. I want to see the files of my application. So i clicked the 'Monitor' button. Then i selected my device from device list and selected my application package.There is a Data folder in file explorer. But when i clicked on the data folder it shows nothing.When i run this application using emulator the file explorer shows the data folder and i can see every files in my application. how can i see my files inside the data folder when using device ?
I found a solution based on the answer in stack overflow .
How to access data/data folder in Android device?
First i rooted the device .Then i modify the access permission of data folder using adb shell.
the code is
$ adb shell
$ su
# mount -o rw
1|root#android:/ # chmod -R 777 data/
Now i can access the files.Thank you #param for your valuable comment.

Android Eclipse DDMS - Can't pull file from rooted device

I am testing my app on a rooted Nexus S phone. I finally have access to the /data/data folder thanks to some awesome help from this answer.
Problem is when I try to use DDMS to "pull a file from the device" I get this error:
[2012-03-20 18:15:26] Failed to pull selection
[2012-03-20 18:15:26] (null)
Anyone know how to pull a copy of the database so I can look at it in a SQLite editor?
I found out that to pull the actual database file (my_database.db) you have to use chmod on that specific file in the adb shell. So you need to explicitly define the path to the database you want to access.
For example, if the database file were my_database.db and the application package was com.example.database, you would need to call the following:
1) In cmd type: adb shell
2) Type: su
3) Press "allow" on the rooted phone
4) In cmd type: chmod 777 /data/data/com.example.database/databases/my_database.db
5) Go to DDMS in Eclipse
6) Navigate to the my_database.db file and select it
7) Choose "pull a file from the device" and choose where to save it
Restart your eclipse once. I hope that will fix this issue

Categories

Resources