Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Modify your <code>createDataBase()</code> method in your <code>DatabaseHandler</code> like the code below:<br/></p> <p><b>createDataBase() :</b></p> <pre><code> public void createDataBase() throws IOException { //If database not exists copy it from the assets boolean mDataBaseExist = checkDataBase(); if(!mDataBaseExist) { this.getReadableDatabase(); this.close(); try { //Copy the database from assests copyDataBase(); System.out.println("createDatabase database created"); } catch (IOException mIOException) { throw new Error("ErrorCopyingDataBase"); } } } </code></pre> <p><b>checkDataBase() :</b></p> <pre><code>private boolean checkDataBase() { File dbFile = new File(DB_PATH + DB_NAME); //Log.v("dbFile", dbFile + " "+ dbFile.exists()); return dbFile.exists(); } </code></pre> <p><b>copyDataBase() :</b></p> <pre><code>//Copy the database from assets private void copyDataBase() throws IOException { InputStream mInput = mContext.getAssets().open(DB_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream mOutput = new FileOutputStream(outFileName); byte[] mBuffer = new byte[1024]; int mLength; while ((mLength = mInput.read(mBuffer))&gt;0) { mOutput.write(mBuffer, 0, mLength); } mOutput.flush(); mOutput.close(); mInput.close(); } </code></pre> <p>Where <code>DB_PATH</code> = <code>/data/data/YOUR_PACKAGE_NAME/Databases/</code>, You can initialize it in your DatabseHandler's Construct like this : <code>DB_PATH = context.getApplicationInfo().dataDir + "/databases/";</code> <br/> <code>DB_NAME</code> = YOUR_DATABASE_NAME_THAT_IS_STORED_IN_YOUR_ASSETS_FOLDER <br/></p> <p>I hope this helps.</p> <p>P.S: DB_NAME and DB_PATH are both Strings.</p> <p><b>UPDATE : </b><br/> Whenever you are trying to use an instance of your <code>DatabaseHelper</code> Class, just call this method <code>createDatabase()</code> like this : </p> <pre><code>DatabaseHandler db = new DatabaseHandler(context); try { db.createDataBase(); } catch (IOException io) { throw new Error("Unable to create database"); } </code></pre> <p>It will copy the existing Database from Asset Folder if the Database has not been copied to the Databases directory.<br/> I hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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