Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - SQLitehelper onCreate overwrites copied database
    primarykey
    data
    text
    <p>Based on <a href="http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/" rel="nofollow">this</a> tutorial and the default notepad example by Google, I tried to create my own Database adapter super class. </p> <p>Now the idea is to copy a pre-created db from my assets folder to my app database folder. I want to use my databasehelper's onCreate method to call the custom method copyDataBase</p> <p>I don't get any IO errors, but what seems to happen is that the database is created, my custom database is copied over and then overwritten again. </p> <p>Any ideas why the onCreate method would overwrite my copied database?</p> <p>Please see my super dbadapter class that contains the custom helper class</p> <pre><code>public abstract class DbAdapter_Super { protected static final String TAG = "MyAppDbAdapter"; protected DatabaseHelper mDbHelper; protected SQLiteDatabase mDb; protected static final String DB_PATH = "/data/data/com.android.myapp/databases/"; protected static final String DB_NAME = "MyAppDB"; protected static final int DB_VERSION = 1; protected final Context mCtx; protected static class DatabaseHelper extends SQLiteOpenHelper { protected final Context mHelpCtx; DatabaseHelper(Context context) { super(context,DB_NAME,null,DB_VERSION); this.mHelpCtx = context; } @Override public void onCreate(SQLiteDatabase db) { //Copy pre-created DB try{ copyDataBase();} catch (IOException e) { throw new Error("Error copying database"); } } @Override public void onOpen(SQLiteDatabase db){}; @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS routes"); onCreate(db); } private void copyDataBase() throws IOException{ //Open your local db as the input stream InputStream myInput = mHelpCtx.getAssets().open(DB_NAME); // Path to the just created empty db String outFileName = DB_PATH + DB_NAME; //if the path doesn't exist first, create it File f = new File( DB_PATH ); if ( !f.exists() ) f.mkdir(); //Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(outFileName); //transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))&gt;0){ myOutput.write(buffer, 0, length); } //Close the streams myOutput.flush(); myOutput.close(); myInput.close(); } } /** * Constructor - takes the context to allow the database to be * opened/created * * @param ctx the Context within which to work */ public DbAdapter_Super(Context ctx) { this.mCtx = ctx; } /** * Open or create the database. * * @return this * @throws SQLException if the database could be neither opened or created */ public DbAdapter_Super open() throws SQLException { mDbHelper = new DatabaseHelper(mCtx); mDb = mDbHelper.getWritableDatabase(); return this; } public void close() { mDbHelper.close(); } } </code></pre> <p>I would really like to keep this abstract super class as is. I just need to understand why the db would be copied over and then overwritten by a blank db. I've tested as much as I can and the db is copied correctly, but overwritten. </p> <p>Any help would be GREATLY appreciated. </p> <p>Thanks</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.
 

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