Domanda

I used the following code

String destDir = "/data/data/" + getPackageName() +
            "/databases/";
    String destPath = destDir + "jobs";
    File f = new File(destPath);
    if (!f.exists()) {
        //---make sure directory exists---
        File directory = new File(destDir);
        directory.mkdirs();
        //---copy the db from the assets folder into 
        // the databases folder---
        try {
            CopyDB(getBaseContext().getAssets().open("jobs"),
                    new FileOutputStream(destPath));
        } catch (FileNotFoundException e) {     
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
public void CopyDB(InputStream inputStream, OutputStream outputStream) 
throws IOException {
    //---copy 1K bytes at a time---
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
        outputStream.write(buffer, 0, length);
    }
    inputStream.close();
    outputStream.close();
}

This copies the database from the assets folder into the data/data database folder and then I can use it but only if its not there.

I need to be able to delete the file in the package and then copy the file from the assets every time the app runs.

I update the data in the database by copying a new database into the assets folder each time and unless I completely un-install the app and re install it the new data is not added.

I want to eventually load the database from a folder on the device so all the user has to do to update is replace the file and the app reads from the new file but that's for later.

È stato utile?

Soluzione

So... just remove this line:

 if (!f.exists()) {

and its corresponding closing bracket (})

This will copy the db ALWAYS, when you run your app.
(It implicitly deletes the file for you, if it is found)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top