9

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.

Ankit Dhadse
  • 1,566
  • 1
  • 15
  • 19
  • http://stackoverflow.com/questions/9109438/how-to-use-an-existing-database-with-an-android-application/9109728#9109728 – Yaqub Ahmad Feb 04 '13 at 11:58

3 Answers3

5
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

Community
  • 1
  • 1
Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
3

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

Navdroid
  • 1,541
  • 3
  • 25
  • 52
  • 1
    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 May 21 '16 at 11:17
  • Should I also do something with ".db-journal" file in there, like remove it? – David Refoua Dec 06 '16 at 22:16
  • These are temporary files created . They will be deleted by itself. – Navdroid Dec 08 '16 at 03:13
1

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.

StarsSky
  • 6,721
  • 6
  • 38
  • 63