sqlite – Database is not being copied in Android-ThrowExceptions

Exception or error:

I create a database on the first run of my app and seed it as well. Sometimes I need to update the database from an outside source so I have to place it in on the Android’s Documents Folder and get it from there. So, I copy that database from Documents folder into the applications database using the code below. Using the debugger, I can finish the whole application without any errors and also on the logs. However, as soon as I check the databse in the application itself in the “data/data/com.myApp.com/databases/dbName.db” This database does not get updated or the database in the Documents folder is not copied. I also tried placing the db to be copied in the application’s “assets” folder but I still don’t get the desired reult:

main_activity

DatabaseHelper db = new DatabaseHelper(getActivity().getApplicationContext());
db.copyDatabase();

DatabaseHelper

public void copyDatabase() {
    DB_FILE = context.getDatabasePath(DB_NAME);
    try {
        InputStream mInput = mContext.getAssets().open(DB_NAME);
        OutputStream mOutput = new FileOutputStream(DB_FILE);
        byte[] mBuffer = new byte[1024];
        int mLenth;
        while((mLenth = mInput.read(mBuffer)) > 0) {
            mOutput.write(mBuffer, 0, mLenth);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
How to solve:

You can create an empty database and then push your database in assets to empty one, as implemented in on the mentioned link:

public class DBHandler extends SQLiteOpenHelper {
protected static final String DATABASE_NAME_PRODUCTION = "productionComments.db";
private Context context;

public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory,
                     int version) {
        super(context, name, factory, DATABASE_VERSION);
        this.context = context;
    }

       //copy database from assets folder (.sqlite) file to an empty database
        public void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = context.getAssets().open("prod.db");

        // Path to the just created empty db
        String outFileName = "/data/data/com.qarun.qpcbeta/databases/"+DATABASE_NAME_PRODUCTION;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the input file to the output file
        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();
    }

NOTE: Instead, of using /data/data/com.qarun.qpcbeta/databases/, you can use getDatabasePath().

Leave a Reply

Your email address will not be published. Required fields are marked *