How can I pull databases off my android onto my desktop?

隐身守侯 提交于 2019-11-30 04:04:13

Accessing internal storage is not possible unless your phone is rooted. One simple way is to use the emulator, and then you can get at the files. For getting a database off the device I wrote a little utility and put in some debug UI for it:

private void backupDb() throws IOException {
    File sd = Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();

    if (sd.canWrite()) {

        String currentDBPath = "/data/com.yourcompany.yourapp/databases/yourapp.db";
        String backupDBPath = "/yourapp_logs/yourapp.db";

        File currentDB = new File(data, currentDBPath);
        File backupDB = new File(sd, backupDBPath);

        if (backupDB.exists())
            backupDB.delete();

        if (currentDB.exists()) {
            makeLogsFolder();

            copy(currentDB, backupDB);
       }

        dbFilePath = backupDB.getAbsolutePath();
   }
}

 private void makeLogsFolder() {
    try {
        File sdFolder = new File(Environment.getExternalStorageDirectory(), "/yourapp_logs/");
        sdFolder.mkdirs();
    }
    catch (Exception e) {}
  }

private void copy(File from, File to) throws FileNotFoundException, IOException {
    FileChannel src = null;
    FileChannel dst = null;
    try {
        src = new FileInputStream(from).getChannel();
        dst = new FileOutputStream(to).getChannel();
        dst.transferFrom(src, 0, src.size());
    }
    finally {
        if (src != null)
            src.close();
        if (dst != null)
            dst.close();
    }
}

Create a "pull.bat" file in your windows desktop.

In the file put:

db pull /data/com.APPNAME/databases/DBNAME.sqlite
pause

If you are using MAC or Linux try this script. This doesn't require root permissions

#!/bin/bash

REQUIRED_ARGS=2
ADB_PATH=/Users/Tadas/Library/sdk/platform-tools/adb
PULL_DIR="~/"

if [ $# -ne $REQUIRED_ARGS ]
    then
        echo ""
        echo "Usage:"
        echo "android_db_move.sh [package_name] [db_name]"
        echo "eg. android_db_move.sh lt.appcamp.impuls impuls.db"
        echo ""
    exit 1
fi;


echo""

cmd1="$ADB_PATH -d shell 'run-as $1 cat /data/data/$1/databases/$2 > /sdcard/$2' "
cmd2="$ADB_PATH pull /sdcard/$2 $PULL_DIR"

echo $cmd1
eval $cmd1
if [ $? -eq 0 ]
    then
    echo ".........OK"
fi;

echo $cmd2
eval $cmd2

if [ $? -eq 0 ]
    then
    echo ".........OK"
fi;

exit 0
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!