23

How do I pull the sqlite database from the android device like emulator?

10 Answers10

57

You can use these commands for pulling the data base from your device. Typing these commands through android studio is very easy. Please make sure you have set the path of adb globally to your system. Then open the "terminal" window from bottom to android studio and near to "Android Monitor". Now you just update the following commands with actual package name of your app and database file name. You will get your file out from the device.

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

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

adb shell "run-as package.name chmod 666 /data/data/package.name/databases/file"
adb exec-out run-as package.name cat databases/file > newOutFileName
adb shell "run-as package.name chmod 600 /data/data/package.name/databases/file"
Manmohan Soni
  • 6,472
  • 2
  • 23
  • 29
  • Perfect. If you want to inspect the data folder first, have a look at this post: http://stackoverflow.com/questions/9017073/is-it-possible-to-see-application-data-from-adb-shell-the-same-way-i-see-it-moun – shadowhorst Jun 26 '15 at 18:04
  • It always gives me file with 0 bytes ?? – Rajeev Kumar Apr 17 '17 at 06:47
  • @Rajeev Kumar -I have update the answer for devices with version greater than Lolipop. One more thing I want to address is that this commonds generally does not works for Samsumg devices. May be samsung devices have some restrictions or issues Please take care before use. – Manmohan Soni Apr 25 '17 at 14:05
9

You can use Android Device Monitor to pull it. Go to Tools -> Android -> Android Device Monitor

  1. Select your device from the left list.
  2. Select FileExplorer tab
  3. Expand data-> data -> "yourpackageName" -> databases
  4. Select your databaseFile and on the top right click on this icon save icon pull a file form a device.
  5. Save it on your desk then use this tool to open it: http://sqlitebrowser.org/
leonardkraemer
  • 6,573
  • 1
  • 31
  • 54
Moaz Rashad
  • 1,035
  • 1
  • 10
  • 16
6
  • connect your device to pc
  • open command prompt in /sdk-directory/tools
  • type adb pull /data/data/com.example.app/databases/database.db

where com.example.app is your application package name and database.db is the database file

that's in case your device is rooted, if not try following

https://stackoverflow.com/a/8433520/1300995

Community
  • 1
  • 1
biegleux
  • 13,179
  • 11
  • 45
  • 52
6

from version 24 onwards i use these commands to pull database

adb shell
run-as package.name
cat /databases/database.db > /sdcard/database.db
exit
exit
adb pull /sdcard/database.db

prior to version 24

adb pull /data/data/package name/databases/database.db

was sufficient

Tushar Saha
  • 1,978
  • 24
  • 29
5

The best way to view and manage you android app database is to use this library https://github.com/sanathp/DatabaseManager_For_Android

Its a single java activity file ,just add the java file to your source folder you can view the tables in your app database , update ,delete, insert rows to you table .Everything from your app.

When the development is done remove the java file from your src folder thats it .

It helped me a lot .Hope it helps you too .

You can view the 1 minute demo here : http://youtu.be/P5vpaGoBlBY

sanath_p
  • 2,198
  • 2
  • 26
  • 22
5

You can skip all those middle stages with one command.

In your PC shell run:

adb -d shell 'run-as <package_name> cat /data/data/<package_name>/databases/<db_name>' > <local_file_name>
Emre
  • 125
  • 1
  • 2
  • 3
3
adb root
adb remount
adb pull <remote>  <local>
Logic Luo
  • 31
  • 2
3

I know this is an old question, however this is what I did to pull the SQLite Database file from app on device.

  1. Saving the file from app package to device sdcard

adb -d shell "run-as com.dautechnology.abdimuna.smartparking cat /data/data/<myAppPackageName>/databases/<databaseFileName> > /sdcard/<databaseFileName>"

The above command will store file to /sdcard You can verify the file by adb shell ls sdcard/ Then exit from adb shell by typing exit

  1. Pull the file from sdcard to your current working location.

adb pull sdcard/<databaseFileName>

  1. Move the file to Desktop or place of your desire.

mv <databseFileName> ~/Desktop

I hope this helps someone out there.

JJD
  • 50,076
  • 60
  • 203
  • 339
abdimuna
  • 775
  • 9
  • 16
2

Do it programatically in application

try {
    File file = Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();

    if (file.canWrite()) {
        String currentPath = "/data/data/" + getPackageName() + "/databases/yourdbname";
        String copyPath = "copydb_name.db";
        File currentDB = new File(currentPath);
        File backupDB = new File(file, copyPath);

        if (currentDB.exists()) {
            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
        }
    }
} catch (Exception e) {

}

Make sure given permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Piyin
  • 1,823
  • 1
  • 16
  • 23
SARATH V
  • 500
  • 1
  • 7
  • 33
1

If you use eclipse then go to DDMS view . from File Explorer Tab ->data->data->application package name->databases-> databse name

select database db file then click pull button

Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37