Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Pulling SQlite database android device

Tags:

android

sqlite

I've looked everywhere and i can't find a real precise answer or a tutorial on how, if it is possible, to do this.

is it possible to pull in any way a database of an Android device without having to root it? i just need to extract that data in any way in order to gather it on my pc, so how can i perform this? do i have to reprogram the app or whatever method you guys know about, but remember without rooting the device. thanks

like image 201
DeX03 Avatar asked Apr 03 '12 17:04

DeX03


People also ask

Where is my SQLite database stored Android?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases.

How do I access SQLite database?

Open a command prompt (cmd.exe) and 'cd' to the folder location of the SQL_SAFI. sqlite database file. run the command 'sqlite3' This should open the SQLite shell and present a screen similar to that below.


2 Answers

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.

like image 86
Sergei Avatar answered Sep 20 '22 19:09

Sergei


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" /> 
like image 20
KarlKarlsom Avatar answered Sep 18 '22 19:09

KarlKarlsom