Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3804398
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:30:43+00:00 2026-05-19T14:30:43+00:00

Based on this tutorial and the default notepad example by Google, I tried to

  • 0

Based on
this tutorial and the default notepad example by Google, I tried to create my own Database adapter super class.

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

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.

Any ideas why the onCreate method would overwrite my copied database?

Please see my super dbadapter class that contains the custom helper class

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))>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();
    }

}

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.

Any help would be GREATLY appreciated.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:30:44+00:00Added an answer on May 19, 2026 at 2:30 pm

    Ok, well I couldn’t force the dbhelper class to work exactely like I wanted. What I instead opted to do was make the copydatabase() method public to my dbadapter class. Then I created a checkdatabase method that simple checks if the db already exists.

    So the trick was to overrride the dbhelper’s onCreate to do nothing. Then, from my dbadapter class I simple checked if the database existed (using the helper’s checkdatabase() method) and if it didn’t, I just called the copydatase method.

    The check method I added to helper class (based on example from link in my first post)

    public boolean checkDataBase(){
    
        SQLiteDatabase checkDB = null;
    
        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
            }
        catch(SQLiteException e){
             //database does't exist yet.
         }
    
         if(checkDB != null){
             checkDB.close();
         }
    
         return checkDB != null ? true : false;
    }
    

    So I then changed the following part my in DBadapter class

    public DbAdapter_Super open() throws SQLException {
           mDbHelper = new DatabaseHelper(mCtx);
    
           //check if db exisist        
           if (mDbHelper.checkDataBase() != true) 
              try{
                  mDbHelper.copyDataBase();
    
                 } 
              catch (IOException e) {
                   throw new Error("Error copying database");
               }
            mDb = mDbHelper.getWritableDatabase();
    
    
           return this;
    }
    

    I’m sure I can still trim and optimize the dbadapter class and do better error handling. But for now, I’m finally able to have a abstract class that I can build table handlers from and it copies my pre-created database.

    Thanks to everyone who posted. Dunno why I didn’t think of it sooner.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writting a keyword search script based on this tutorial: http://www.hackosis.com/2007/11/06/howto-simple-search-engine-with-php-and-mysql/ Like
I'm building an ogre3d tutorial based on this setup http://www.ogre3d.org/wiki/index.php/SettingUpAnApplication#XCode I've followed all steps
Based on this question it appears that the default template for CheckStyle will allow
I want to create dynamic content based on this. I know it's somewhere, as
Based on this question I don't want to litter my ready stuff waiting for
I have up to 4 files based on this structure (note the prefixes are
Based on the response to this question: Why does C++ have header files and
This question is based on another question of mine (thankfully answered). So if in
This request is based in MS Access VBA. I would like to know what
By this, I don't mean PC-based development environments (like Visual Studio) for developing and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.