4

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.

Dan
  • 3,182
  • 1
  • 27
  • 27

1 Answers1

5

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

Community
  • 1
  • 1
Anton Derevyanko
  • 3,405
  • 1
  • 24
  • 32
  • 1
    Thanks, I ended up doing something like this. I just hated the fact that all of these examples use SQLiteOpenHelper but dont use the onDatabaseCreate (or the other interface methods) for its implied purpose. I guess that method just assumes you are using the automatically created database. – Dan Apr 21 '11 at 17:12