Android - Pulling SQlite database android device Android - Pulling SQlite database android device sqlite sqlite

Android - Pulling SQlite database android device


If your device is running Android v4 or above, you can pull app data, including it's database, without root by using adb backup command, then extract the backup file and access the sqlite database.

First backup app data to your PC via USB cable with the following command, replace app.package.name with the actual package name of the application.

adb backup -f ~/data.ab -noapk app.package.name

This will prompt you to "unlock your device and confirm the backup operation". Do not provide a password for backup encryption, so you can extract it later. Click on the "Back up my data" button on your device. The screen will display the name of the package you're backing up, then close by itself upon successful completion.

The resulting data.ab file in your home folder contains application data in android backup format. To extract it use the following command:

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

If the above ended with openssl:Error: 'zlib' is an invalid command. error, try the below.

dd if=data.ab bs=1 skip=24 | python -c "import zlib,sys;sys.stdout.write(zlib.decompress(sys.stdin.read()))" | tar -xvf -

The result is the apps/app.package.name/ folder containing application data, including sqlite database.

For more details you can check the original blog post.


A common way to achieve what you desire is to use the ADB pull command.

Another way I prefer in most cases is to copy the database by code to SD card:

try {    File sd = Environment.getExternalStorageDirectory();    if (sd.canWrite()) {        String currentDBPath = "/data/data/" + getPackageName() + "/databases/yourdatabasename";        String backupDBPath = "backupname.db";        File currentDB = new File(currentDBPath);        File backupDB = new File(sd, backupDBPath);        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) {}

Don't forget to set the permission to write on SD in your manifest, like below.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


For debuggable apps1 on non-rooted devices, you could use following command:

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

Example:

adb shell run-as com.app chmod 666 /data/data/com.app/databases/data.dbadb pull /data/data/com.app/databases/data.db

Set PATH adb for Enviroment Variables or use cd command to android sdk folder platform-tools.

Example:

cd /folder/android-sdk/platform-tools/

then use above command


1 Note that most apps in Play store are not debuggable since it requires setting the debuggable flag in the manifest.