Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 861
Ankit Dhadse Avatar asked Feb 04 '13 11:02

Ankit Dhadse


2 Answers

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

like image 140
Yaqub Ahmad Avatar answered Sep 30 '22 18:09

Yaqub Ahmad


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

like image 32
Navdroid Avatar answered Sep 30 '22 20:09

Navdroid