How to replace existing sqlite database file with new database file in android
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.
![Ankit Dhadse](https://i.stack.imgur.com/jvvBb.jpg?s=256&g=1)
Ankit Dhadse
A person who is willing to learn new technologies and likes to help other.
Updated on June 14, 2022Comments
-
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 about 8 yearsHe 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 about 8 yearsWorked 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 over 7 yearsShould I also do something with "<database>.db-journal" file in there, like remove it?
-
Navdroid over 7 yearsThese are temporary files created . They will be deleted by itself.
-
Jesus Almaral - Hackaprende over 5 yearsThe new db file acts as if it was empty but it is not, I cannot find any data on it
-
Snake over 5 years@Jesus , this issue started hapening recently, did you find a way around it