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

流过昼夜 提交于 2019-11-29 01:12:46

问题


I'm trying this with my Nexus One. I have the android SDK and have used the command adb pull /data/data/com.myapp.android/databases C:\pulls but all I get is pull: building file list... 0 files pulled. 0 files skipped. Also, it seems no matter how much data I add to the tutorial NotePad app I installed, the data size for the app (as shown in Settings) never exceeds 8KB. How is this possible? Is there some other place where databases are stored? When I use the File Explorer view (that's part of ADT) in Eclipse, I see there's nothing in /data. To add a twist, I have no trouble pulling any other files from the device. It's just databases I have trouble with. Am I missing something? Thanks much.


回答1:


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();
    }
}



回答2:


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

In the file put:

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



回答3:


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


来源:https://stackoverflow.com/questions/4202064/how-can-i-pull-databases-off-my-android-onto-my-desktop

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