Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let say you have database named "<code>prepared.db</code>" and your package name is "<code>com.example.android</code>". This is what I do.</p> <ol> <li>make sure you prepared database is under assets folder. </li> <li>In the constructor of class that extends OrmLiteSqliteOpenHelper, check whether or not database exist. </li> <li>If database <strong>DID NOT exist</strong>, copy db from assets folder</li> </ol> <p>(Both Step 2 and 3 happen inside constructor)</p> <p>This is code sample and It work for me.</p> <pre><code>public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private static final String DATABASE_NAME = "prepared.db"; private static final String DATABASE_PATH = "/data/data/com.example.android/databases/"; public DatabaseHelper(Context context) { super(context, DATABASE_PATH+DATABASE_NAME, null, DATABASE_VERSION); boolean dbexist = checkdatabase(); if (!dbexist) { // If database did not exist, try copying existing database from assets folder. try { File dir = new File(DATABASE_PATH); dir.mkdirs(); InputStream myinput = mContext.getAssets().open(DATABASE_NAME); String outfilename = DATABASE_PATH + DATABASE_NAME; Log.i(DatabaseHelper.class.getName(), "DB Path : " + outfilename); OutputStream myoutput = new FileOutputStream(outfilename); byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer)) &gt; 0) { myoutput.write(buffer, 0, length); } myoutput.flush(); myoutput.close(); myinput.close(); } catch (IOException e) { e.printStackTrace(); } } } /* * Check whether or not database exist */ private boolean checkdatabase() { boolean checkdb = false; String myPath = DATABASE_PATH + DATABASE_NAME; File dbfile = new File(myPath); checkdb = dbfile.exists(); Log.i(DatabaseHelper.class.getName(), "DB Exist : " + checkdb); return checkdb; } } </code></pre> <p>P.S : If the database file size is more than 1mb, error will occur. You can <a href="http://androidsnips.blogspot.com/2010/10/copying-larger-database-files-in.html">split database to solve</a> such issue.</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