How to replace existing sqlite database file with new database file in android

13,816

Solution 1

private void importDatabase(String inputFileName) throws IOException
{
    InputStream mInput = new FileInputStream(inputFileName);
    String outFileName = YOUR_DB_PATH_HERE;
    OutputStream mOutput = new FileOutputStream(outFileName);
    byte[] mBuffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(mBuffer))>0)
    {
        mOutput.write(mBuffer, 0, mLength);
    }
    mOutput.flush();
    mOutput.close();
    mInput.close();
}

EDIT:

You may find it helpfull:

How to use an existing database with an Android application

Solution 2

Simply you could download the db file from URL using Download Manager and copy the file to this path

         /data/data/<YOUR PACKAGE NAME>/databases/

It will automatically update.I have used this and is working

Solution 3

At the first launch of your application, you should copy your database into external/internal storage, out of the apk file. When you need to update (with statements or by replacing the whole db) you can operate in the internal/external memory without problems. When you update your application (and maybe the new application has a new db inside), at the first launch of the new app, you should replace the db you stored in the internal/external memory.

Share:
13,816
Ankit Dhadse
Author by

Ankit Dhadse

A person who is willing to learn new technologies and likes to help other.

Updated on June 14, 2022

Comments

  • Ankit Dhadse
    Ankit Dhadse about 2 years

    Im doing an app which uses sqlite database There is an feature in my app having a button 'Update Database'.

    When user clicks on the 'Update Database' button i need to upgrade the old db file with new db file from URL.

    How to do this. the research tells me that we cant change a db as it gets place in an .apk file. Is there any solution to this. Please help me out. Thank you.

  • StarWind0
    StarWind0 about 8 years
    He is saying to copy the data directly into the directory for the application. The foo.db file for your app is in the directory pattern above. However in my case, I do not have a rooted device so I will not be able to access the databse folder. You might be able to just make a new one. I solved it with simple IO, the the other answer.
  • StarWind0
    StarWind0 about 8 years
    Worked like a charm! Note for others. I allowed for restoration with the exact same code. Its a simple copy this file function after all.
  • David Refoua
    David Refoua over 7 years
    Should I also do something with "<database>.db-journal" file in there, like remove it?
  • Navdroid
    Navdroid over 7 years
    These are temporary files created . They will be deleted by itself.
  • Jesus Almaral - Hackaprende
    Jesus Almaral - Hackaprende over 5 years
    The new db file acts as if it was empty but it is not, I cannot find any data on it
  • Snake
    Snake over 5 years
    @Jesus , this issue started hapening recently, did you find a way around it