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

Im making an application that uses sqlite database. In my application, there is a function with the "Update Database" button.

When the user clicks the "Update Database" button, I need to update the old db file with the new db file at the URL.

How to do it. research tells me that we cannot change db because it gets into the .apk file. Is there any solution to this. Please help me. Thanks.

+6
source share
3 answers

You can simply download the db file from the URL using the download manager and copy the file to this path

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

It will be automatically updated. I used it and it works

+3
source
 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 this helpful:

How to use an existing database with an Android application

+3
source

When you start the application for the first time, you must copy your database to external / internal memory from the apk file. When you need to update (using operators or by replacing the whole db), you can work in internal / external memory without problems. When you update your application (and perhaps the new application has a new db inside), the first time you start a new application, you should replace the db that you saved in internal / external memory.

0
source

All Articles