Android - Pulling SQlite database android device
Solution 1:
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.
Solution 2:
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" />
Solution 3:
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/file
adb pull /data/data/package.name/databases/file
Example:
adb shell run-as com.app chmod 666 /data/data/com.app/databases/data.db
adb 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.
Solution 4:
Most of the answers here are way more complicated than they have to be. If you just want to copy the database of your app from your phone to your computer then you just need this command:
adb -d shell "run-as your.package.name cat databases/database.name" > target.sqlite
All you need to fill in in the above command is the package name of your app, what the database is called and optionally what/where you want the database to be copied to.
Please note that this specific command will only work if you have only one device connected to your computer. The -d
parameter means that the only connected device will be targeted. But there are other parameters which you can use instead:
-
-e
will target the only running emulator -
-s <serialnumber>
will target a device with a specific serial number.