0

My question is that I want to restore my application's sqlite database after I overwrite the same app on my device. I don't like to add settings again and again on start up of my app.

So is it possible in android to save the database somewhere from which I can again restore it ?

I have searched for more than hours on Google and SO but couldnt ind any solution.

EDIT: Its not a fixed database. So I can't store it in Assets Folder. It is editable by user but by default it should carry the last edited values(values before the app overwrite).

6
  • Question not very clear. Databases do not get removed when updating apps so I don't see why you need to restore it from somewhere after doing so. Commented Jul 30, 2012 at 12:45
  • If you just update your app using the same key for signing, the database should still exist, afaik. Maybe when opening your databasse after overwriting your onUpdate() clears the database? - just a thought, may be false... Commented Jul 30, 2012 at 12:45
  • @Kuffs- I am completely removing my app ad then again installing..so I lost my sqlite database every time..I need to restore that. Commented Jul 30, 2012 at 12:47
  • some of the tutorials I have seen create a new database each time the app is installed. Make sure your 'create database' method doesn't do that, otherwise, Kuffs has your answer Commented Jul 30, 2012 at 12:48
  • @Martze- Yes, you r right. But I am not updating my app. I am overwriting it by first completely removing it. That's the problem. Commented Jul 30, 2012 at 12:49

2 Answers 2

1

This method I find very helpful:

public static void movedb(File srcdb, File destdb)
{
    try 
    {
        if (Environment.getExternalStorageDirectory().canWrite()) 
        {                 
            if (srcdb.exists()) 
            {
                FileChannel src = new FileInputStream(srcdb).getChannel();
                FileChannel dst = new FileOutputStream(destdb).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();                    
            }
            else
            {
                //ERROR: "Database file references are incorrect"                    
            }
        }
        else
        {
            //ERROR: "Cannot write to file"
        }
    }
    catch (Exception e) 
    {
        //ERROR: e.getMessage()
    }
}

Then I just call

movedb(this, new File(<context>.getDatabasePath("...your DB name...")), new File("... your location ..."));

To back up, and then to restore:

movedb(this, new File("... your location ..."), new File(<context>.getDatabasePath("...your DB name...")));
Sign up to request clarification or add additional context in comments.

5 Comments

But I wonder how will I have srcdb file if I uninstall my app ? I don't have exact idea about this. Thanks for reply.
You can use this code to make a copy of your database (in a file in your external storage directory) before you uninstall. Then when you reinstall you can restore the data base.
And the data gets store on SD Card. Right ??
@QuintinBalsdon how do i set the file location when creating a backup? As in what will that value be since I don't know how the directory structure on my phone is
The file location is a directory you know to exist on the device. You can check what directories are on Environment.getExternalStorageDirectory() by printing it to your logcat.
0

I'm using ORMLite and, apart from storing the database in the external public directory, after I restore the file to the database directory, I have to re-instantiate the DatabaseHelper singleton and create a new one.

Here is my version, omitting every try/catch block for the sake of simplicitiy:

public boolean restoreBackup(Context context){

    String databasePath = "data/data/my.package.name/databases/myDatabase.sqlite";
    String backUpPath = context.getDatabaseDir("myDatabase.sqlite");

    // Copies back-up to database directory
    new File(databasePath).delete();
    FileInputStream streemToBackUp = new FileInputStream(new File(backUpPath));
    OutputStream streamToDatabaseFile = new FileOutputStream(databasePath);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = streamToBackUp.read(buffer)) > 0) {
        streamToDatabaseFile.write(buffer, 0, length);
    }
    streamToDatabaseFile.flush();
    streamToDatabaseFile.close();
    streamToBackUp.close();

    // Re-instantiate DatabasHelper singleton
    DatabaseHelper.closeHelper();
}

The body of closeHelper() is as follows:

public static void closeHelper() {
    helper.close();
}

@Override
public void close() {
    super.close();
    myDao = null; // Set to null every day you have
    helper = null; // Set to null the singleton instance of the helper
}

This will work as long as you don't use OpenHelperManager class to instantiate the helper, and always use getHelper() whenever you need the database instead of storing the instance returned.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.