Backup/restore sqlite db in android

Possible Duplicate:
Android backup/restore: how to backup an internal database?

I am developing an app in which there is SQL lite db. I need to give the user the ability to back up the database and restore it again. How can I do that? Sample code?

  • Android emulator doesn't take keyboard input - SDK tools rev 20
  • Android: failed to allocate memory
  • Call requires API level 11(current min is 9) android.app.Activity#onCreateView
  • Get screen width and height in a Fragment
  • How do I use Android ProgressBar in determinate mode?
  • Detecting the end of navigation in Waze or Google Maps for mobile
  • And in case my user saved the db, and then upgraded the app to a new DB version code, will restore work? How do I go about that?

    Please help

    Related posts:

    Override back button to act like home button
    How to move an image from left to right in android
    Crop a fixed size image in Android
    Android: ScrollView Issue
    Android List View set default position without animation
    Using mockito to test function that uses a context
  • Fatal Error: Invalid Layout of java.lang.String at value
  • Rotate camera preview to Portrait Android OpenCV Camera
  • Keycode for Android minimize soft keyboard button
  • Android - Failed to find provider info for com.google.settings in MapView Example
  • Android: java rounding error. Can't understand why?
  • Only relayout children and not all the tree
  • 2 Solutions collect form web for “Backup/restore sqlite db in android”

    In the folder “/data/data/’your.app.package’/databases/” you have a .db file that is your database. You can copy that file, save it, and then place it back there again.

    One example on how to backup the database to the external storage:

        final String inFileName = "/data/data/<your.app.package>/databases/foo.db";
        File dbFile = new File(inFileName);
        FileInputStream fis = new FileInputStream(dbFile);
    
        String outFileName = Environment.getExternalStorageDirectory()+"/database_copy.db";
    
        // Open the empty db as the output stream
        OutputStream output = new FileOutputStream(outFileName);
    
        // Transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer))>0){
            output.write(buffer, 0, length);
        }
    
        // Close the streams
        output.flush();
        output.close();
        fis.close();
    

    Can you clarify what you mean by “back up”? SQLite doesn’t have a specific backup method; you have to read rows from the db and save them somehow. Many developers use XML to do this.

    If the user saves the db and you then move to a new DB version code, you have to decide how you’ll do the restore. Again, you have to do the work to read the backup and put it back into the database; neither SQLite nor Android does this for you.

    The package android.app.backup contains classes for working with the Android Backup Agent, but they implement a general framework. It’s up to you to implement the specifics for each file or database you’re backing up.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.