How can I pull databases off my android onto my desktop?
Asked Answered
P

3

6

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.

Picklock answered 17/11, 2010 at 6:34 Comment(0)
L
10

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();
    }
}
Luca answered 17/11, 2010 at 6:42 Comment(5)
Thanks for sharing your code. Initially I used the emulator but switched to developing on my Nexus One Dev phone because it's way way faster that way. What's weird is my Nexus One is rooted. When I boot up I see the unlocked padlock at the bottom of the screen while Android loads. Doesn't this mean I should be able to execute root commands?Picklock
You code looks fine, but my Eclipse doesn't recognise the copy(currentDB, backupDB); instruction. Do you have some extra library (.jar) installed to use the copy method?Staging
Hey dhaag23, could you please provide the copy method implementation?Closelipped
Hi Bram - I've updated the code to include the copy() method.Luca
hi @dhaag23, the line dbFilePath = backupDB.getAbsolutePath(); gives me an error... what is dbFilePath?Surfeit
P
0

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

In the file put:

db pull /data/com.APPNAME/databases/DBNAME.sqlite
pause
Paderna answered 4/9, 2012 at 14:39 Comment(0)
E
0

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
Esqueda answered 9/4, 2013 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.