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

  • SEARCH
  • Home
  • 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 4537454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:40:41+00:00 2026-05-21T14:40:41+00:00

I have my database stored in my assets folder and copied over at run

  • 0

I have my database stored in my assets folder and copied over at run time. I currently have a simple Activity that makes a simple database call:

    DBAdapter adapter = new DBAdapter(HomeActivity.this);
    final SQLiteDatabase db = adapter.getReadableDatabase();
    final Cursor c = db.query("exercises", new String[] { "name" }, null,
            null, null, null, null);
    startManagingCursor(c);
    if (c.moveToFirst())
        Log.e(TAG, c.getString(0));
    else
        Log.e(TAG,"No dice");

And below is my DBAdapter.java (which extends open helper):

public class DBAdapter extends SQLiteOpenHelper {

    private static final int DB_VERSION = 1;
    private static String DB_PATH = "";
    private static final String DB_NAME = "gymrat.db";
    private final Context myContext;
    private static final String TAG = "GymRat.DBAdapter";

    /**
     * Constructor Takes and keeps a reference of the passed context in order to
     * access to the application assets and resources.
     * 
     * @param context
     */
    public DBAdapter(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;
        DB_PATH = "/data/data/"
                + context.getApplicationContext().getPackageName()
                + "/databases/";
    }

    /**
     * Copies your database from your local assets-folder to the just created
     * empty database in the system folder, from where it can be accessed and
     * handled. This is done by transferring bytestream.
     * */
    private void copyDatabase() throws IOException {
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    /**
     * Call on creating data base for example for creating tables at run time
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.e(TAG, "onCreate");
        try {
            copyDatabase();
        } catch (IOException e) {
            Log.e(TAG,"IOException copying Database");
        }
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        Log.e(TAG, "onOpen");

        try {
            db.rawQuery("select * from exercises", null);
        } catch ( SQLiteException e) {
            Log.e(TAG,"DB copy didn't work");
            try {
                copyDatabase();
            } catch (IOException e1) {
                Log.e(TAG,"IOException recopying DB");
            }
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.e(TAG, "Upgrading");
    }
}

I took this adapter from here and modified it since it didn’t seem to use the OpenHelper functionality.

My Problem: Whenever my app is run for the first time after installing, or after wiping data on the app, onCreate gets called first as I would expect, and calls the copyDatabase method. The problem is that the database does not get copied — or if it does it is immediately overwritten. Instead a default database with only an “android meta_data” table is created, causing the query in my activity to throw an exception because the table doesn’t exist.

If I copy the database again in the onOpen method it works fine. Am I doing something out of order or missing some call that is causing the SQLiteOpenHelper to create a default database? The default database created uses the same name and version number specified in the constructor.

Right now, as you can see, I am resorting to using a dummy query in onOpen to see if the expected table exists and if not going ahead and recopying the database. Another option is just setting a flag when onCreate is called signalling onOpen to copy the database over. Obviously these are both a bit hacky and I’m really curious what is going on.

I plan on moving the actual database calls to separate helper classes outside of the Activity, I was merely calling the db directly to test it.

  • 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-21T14:40:42+00:00Added an answer on May 21, 2026 at 2:40 pm

    If you want to copy database from assets at first time running and to open database from data/data/program.name/database, you should use the code something like this:

    public class YourDatabase extends SQLiteOpenHelper
    
    public YourDatabase(Context context) {
        super(context, DatabaseName, null, DATABASE_VERSION);
        this.dbContext = context;
        // checking database and open it if exists        
        if (checkDataBase()) {
        openDataBase();
        } else {
        try {
                this.getReadableDatabase();
                copyDataBase();
            this.close();
            openDataBase();
    
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
         Toast.makeText(context, "Initial database is created", Toast.LENGTH_LONG).show();
        }
    }
    

    Full answer you can read from this question

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

Sidebar

Related Questions

I've got an MS-Access database stored in a folder (yes, I have 'Modify' permissions
I have a database that is stored locally on my desktop computer. I've made
I have an SQLite database stored in the assets resources of one application used
I have database where data is stored along timestamp entries, that are keys. i
I'm trying to do a simple call to a database stored procedure from a
I have sql database stored on a shared netwrok drive , after set of
Can I host an application in Windows Azure and have the database stored on
I have an SQL database stored in my server, and I want my iOS
In the past I have stored database credentials (username, password) in another file (outside
I have a very odd issue. When I execute a specific database stored procedure

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.