Get and copy SQLite .db file on Android using ADB

1

I have an application in android that makes use of the class SQLiteOpenHelper to work a local database with 3 simple tables, to get the file MY_DATABASE.db I use commands by ADB of the next Form:

$ adb shell
shell@device:/ $ run-as com.my.package
shell@device:/data/data/com.my.package $ cp databases/MY_DATABASE /sdcard/MY_DATABASE.db
shell@device:/data/data/com.my.package $ exit
shell@device:/ $ exit
adb pull /sdcard/MY_DATABASE.db

My problem is that these commands do not work on all the devices I've tried:

  

Moto E (with root and without this) WORKS
  Moto G (with root and without this) WORKS
  Nexus 5x (with root and without this) DOES NOT WORK
  Samsung J1 (without root) DOES NOT WORK
  Samsung J2 (without root) DOES NOT WORK
  Huawei (without root) DOES NOT WORK

What can I do to make the commands work for me on any device?

I managed to do this without any problem on all the devices mentioned above with this function:

public void copy(File src, File dst) throws IOException {
    Log.d(TAG, "copying...");
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
    Log.d(TAG, "copied");
}

String strSrc = "/data/data/com.my.package/databases/MY_DATABASE"
String strDst = "/sdcard/MY_DATABASE.db";

File fSrc = new File(strSrc);
File fDst = new File(strDst);

try{
    copy(fSrc, fDst);
}catch(IOException e){
    Log.e(TAG, e.toString());
}

However I want to be able to do this process by ADB

    
asked by Jorius 18.11.2016 в 17:00
source

1 answer

1

That the terminal is rooted or is not indifferent when using the run-as command. If you want to use the special privileges of the user root to perform the copy you must first type the command su (and accept raise the permissions in your root manager) and then make the cp to copy the databases .

Not all terminals support run-as or they work correctly. I've seen in forums where I was not available or had incorrect permissions and have had to go up as rw file system /system and adjust permissions correctly with chmod 4750 /system/bin/run-as ( see this example ).

In other cases you may have problems with the application's permissions. As run-as gives you exactly the same permissions for a particular application, it is likely that this application does not have permissions to write in the microSD, so it will give you permission denied when copying in /sdcard .

In modern versions of Android applications can not write to the root of the microSD without the help of the system application Documentos (and in many terminals this is disabled by default).

You must give me exactly the error message that prevents you from using run-as to determine in which case you are. A simple WORKS or DOES NOT WORK does not help in solving your problem.

    
answered by 18.11.2016 / 20:13
source