Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using the tips of both hovanessyan and Damian (remind me to rep+1 you as soon as I reach 15 ;), I came up with the following solution:</p> <ol> <li>For relatively small databases (&lt;1,5Mb)</li> </ol> <p>I created the database using SQLite Database Browser, and put it in my Assets folder.</p> <p>Then, the following code copies the database to the device, if it's not already there:</p> <pre><code> boolean initialiseDatabase = (new File(DB_DESTINATION)).exists(); </code></pre> <p>public void copyDB() throws IOException{ final String DB_DESTINATION = "/data/data/happyworx.nl.Flitswoorden/databases/WoordData.db";</p> <pre><code> // Check if the database exists before copying Log.d("Database exist", "" + initialiseDatabase); Log.d("Base Context", "" + getBaseContext()); if (initialiseDatabase == false) { // Open the .db file in your assets directory InputStream is = getBaseContext().getAssets().open("WoordData.db"); // Copy the database into the destination OutputStream os = new FileOutputStream(DB_DESTINATION); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) &gt; 0){ os.write(buffer, 0, length); } os.flush(); os.close(); is.close(); }} </code></pre> <p>In my app, a portion of the database is User-customizable.</p> <p>I call the code above in onStart() with :</p> <pre><code> try { copyDB(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p>So, when the user presses "reset database to standard" (in preferences screen), I just set the Boolean <code>initialiseDatabase</code> to "false" and wait for the user to go back to the main activity. (thus calling onstart and copying the original database).</p> <p>I tried to call the Activity.copyDB() from the preferences.java. It's neater, because it doesn't require the user to go back to the main activity to rebuild the database. However, I get an error about not being able to call static references to non-static methods. I don't understand that, but will look into it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload