Get Database File in /Data/Data on Rooted Device

Get database file in /data/data on rooted device


cat source_file > dest_file

You can also install busybox, and get the use of cp command, along with a host of others.
https://play.google.com/store/apps/details?id=stericson.busybox&hl=en

On Rooted phone read /data/data/****/**.db

Apps, regardless of whether the phone is rooted or not, can only ever read files that they themselves own, or that are public (e.g., on a SD card). This is because while the phone may be rooted, the apps themselves do not gain root access.

Instead, on your phone, you have an executable named su lets apps run root commands. However, by default, it refuses to let any app run any root commands. When you root your phone, you replace this executable by a new, modified version that lets approved apps run root commands. It is by using these root commands that you can gain indirect root access to the system.

Now, since you only have indirect root access for your app, you cannot just read any file from the file system. But if you run a root command to copy it to your own, private directory, where you do have permission to read it, your app can directly read it from there.

(Note: you can technically read files without copying them first, by using the su executable, but unless there's a real reason why you can't copy first, and you actually know what you're doing, you probably shouldn't even bother because it's rarely worth the trouble anyways, especially not for sqlite databases.)

For details about how to run root commands with su, see this link (which Gumbo posted in the comments above).

How to access data/data folder in Android device?

Accessing the files directly on your phone is difficult, but you may be able to copy them to your computer where you can do anything you want with it.
Without rooting you have 2 options:

  1. If the application is debuggable you can use the run-as command in adb shell

    adb shell
    run-as com.your.packagename
    cp /data/data/com.your.packagename/
  2. Alternatively you can use Android's backup function.

    adb backup -noapk com.your.packagename

    You will now be prompted to 'unlock your device and confirm the backup operation'. It's best NOT to provide a password, otherwise it becomes more difficult to read the data. Just click on 'backup my data'. The resulting 'backup.ab' file on your computer contains all application data in android backup format. Basically it's a compressed tar file. This page explains how you can use OpenSSL's zlib command to uncompress it.
    You can use the adb restore backup.db command to restore the backup.

How to view database on root device ?

you can copy your sqlite database with adb from your device

  1. connect your phone to pc (usb, debug enabled...)
  2. adb devices list your connected phones
  3. adb -s deviceName pull /data/data/your.app.name.space/databases/your-db-name.db

if you don't know hows the name of your db use the adb shell

adb -s deviceName shell

than list the files in your databases folder like this

ls /data/data/your.app.name.space/databases/

than you have the sqlite database you can browse with some sqlite tools

How to check database on not rooted android device

You can write your database to the external memory with the following:

private void writeToSD() throws IOException {
File sd = Environment.getExternalStorageDirectory();

if (sd.canWrite()) {
String currentDBPath = DB_NAME;
String backupDBPath = "backupname.db";
File currentDB = new File(DB_PATH, 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();
}
}
}

Where DB_NAME is the name of my database and DB_PATH is defined as follows:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DB_PATH = context.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
}
else {
DB_PATH = context.getFilesDir().getPath() + context.getPackageName() + "/databases/";
}

And add the following permission (Thanks to @Sathesh for pointing this out):

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

I call this method anytime I have a database write so that my most current database file is in the external memory and I can view it and debug from there.

Then you can use the X-Plore app to view the database from the external memory right on the Android device.

Retrieve database or any other file from the Internal Storage using run-as

By design user build of Android (that's what you have on your phone until you unlock the bootloader and flash the phone with userdebug or eng software) restricts access to the Internal Storage - every app can only access its own files. Fortunately for software developers not willing to root their phones Google provides a way to access the Internal Storage of debuggable versions of their packages using run-as command.

To download the /data/data/debuggable.app.package.name/databases/file from an Android 5.1+ device run the following command:

adb exec-out run-as debuggable.app.package.name cat databases/file > file

To download multiple files in a folder under the /data/data/debuggable.app.package.name/ at once - use tar:

adb exec-out run-as debuggable.app.package.name tar c databases/ > databases.tar
adb exec-out run-as debuggable.app.package.name tar c shared_prefs/ > shared_prefs.tar

How to find database file generated of my application in android device

You will only be able to access the database on a physical device if your device is rooted. An emulator gives full access to the databases, where as a device does not, unless it is rooted.

View contents of database file in Android Studio

Finally, i found a simplest solution which do not need to open the DDMS.

Actually, the solution is based on what @Distwo mentioned, but it don't have to be that complicated.

First, remember the path of your database file in the device, it should aways the same.
For example mine is:/data/data/com.XXX.module/databases/com.XXX.module.database

Second, execute this command which pulls your database file onto you PC

 adb pull /data/data/com.XXX.module/databases/com.XXX.module.database /Users/somePathOnYourPC/

What you need to do is just copy and store this command, then you can use it again and again.

Third, if you got Permission Denied or something like that, just run adb root before the previous command.



Related Topics



Leave a reply



Submit