3

I am using a sqlite database in my android app. I want to download the database to pc. Can anyone kindly tell me what to do ?

3 Answers 3

6

if your app is in emulator you can just use the DDMS and open /data/data/your.package.name/databases.

if you have your app in mobile. here is what i do to copy the db to the sdcard root folder.

    /** The name of the database file */
    static final String DATABASE_NAME = "mydatabase.db";
    static final String DATABASE_NAME_FULL = "/data/data/com.my.application/databases/" + DATABASE_NAME;

    public static boolean backUpDataBase(Context context){
    boolean result = true;

    // Source path in the application database folder
    String appDbPath = DATABASE_NAME_FULL;

    // Destination Path to the sdcard app folder
    String sdFolder =  Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + DATABASE_NAME;


    InputStream myInput = null;
    OutputStream myOutput = null;
    try {
        //Open your local db as the input stream
        myInput = new FileInputStream(appDbPath);
        //Open the empty db as the output stream
        myOutput = new FileOutputStream(sdFolder);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }
    } catch (IOException e) {
        result = false;
        e.printStackTrace();
    } finally {
        try {
            //Close the streams
            if(myOutput!=null){
                myOutput.flush();
                myOutput.close();
            }
            if(myInput!=null){
                myInput.close();
            }
        } catch (IOException e) { } 
    }

    return result;
}

you need this in your manifest.xml

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

If your emulator is running you can access it by opening the DDMS perspective (Window > Open perspective > DDMS) and open data/data/your.package.name/databases and pulling it onto your computer.

You can't get it from your device if it is not rooted. If it is you can't access the directory where it is located because it is protected. You have to copy it with a root explorer onto your SD card and then you can pull it with DDMS onto your computer.

2
  • nope, its not rooted, what do i do now ? :( ..btw, the db is the db of my own app....
    – ahsan
    Sep 28, 2011 at 1:04
  • Then your out of luck I think. The app data is private - for a good reason. I think there's no chance for you...
    – Knickedi
    Sep 28, 2011 at 1:10
0

I solved this by using the android debug bridge. To get the database you have to execute in the directory of your sdk tools:

adb.exe exec-out run-as com.example.project cat databases/databasename > file.sqlite

where com.example.project is the packagename from the manifes and databasename is the name of your database. The database will be put to your current directory.

It works on emulators and devices (checked up to API Level 28).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.