how to download the sqlite database from an android application?

advertisements

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 ?


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" />