how to update database by sqlite file downloaded from internet at runtime?

259 Views Asked by At

I have sqlite file in assets and dbflow read it correctly but at runtime I sometimes need to download a new updated sqlite file from internet and place it in certain folder in sdcard so how to update dbflow database by this sqlite file in sdcard to remove data of all tables and add the new data from the file at sdcard to them ?

1

There are 1 best solutions below

0
On

Try the BElow Code. This is a backup I had imported from Application for Restoring Data after Uninstall. Try the code. Hope it Works

NB : PROVIDE READ AND WRITE PERMISSION ON RUNTIME AND IN MANIFEST TOO

private void importDB() {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();
        if (sd.canWrite()) {
            String currentDBPath = DOWNLOADEDDATAPATH; //SOURCE
            String backupDBPath = "data/" + getContext().getPackageName() + "/databases/FILENAME";  //Destination


            File backupDB = new File(sd, currentDBPath);  //SOURCE
            File currentDB = new File(data, backupDBPath);  //Destination

            FileChannel src = new FileInputStream(backupDB).getChannel();  //SOURCE
            FileChannel dst = new FileOutputStream(currentDB).getChannel(); //Destination
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();


        }
    } catch (Exception e) {
        Log.d("test", e.toString());
    }
}