0

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();
    }
}
Ibanez1408
  • 4,550
  • 10
  • 59
  • 110

1 Answers1

0

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().

Arsal Imam
  • 2,882
  • 2
  • 24
  • 35
  • I'll try that Sir... When you say "empty database:, since my database has data in it before the copying of data occurs, does it mean I have to empty my database first Or delete my database first then recreate the database, then fill it with data from the database in the assets folder? – Ibanez1408 Feb 24 '20 at 07:19
  • the above method will take care of this `empty database`, you just need to put your filled database in assets – Arsal Imam Feb 24 '20 at 07:32
  • So I just have to drop all my tables then so I'll be empty. – Ibanez1408 Feb 24 '20 at 07:35
  • which database you're talking about, one which is in your app data folder, or assets one? – Arsal Imam Feb 24 '20 at 07:37
  • The application's database. The one that is created on the first run of the application. "/data/data/com.qarun.qpcbeta/databases/mtc.db" – Ibanez1408 Feb 24 '20 at 07:39
  • Yes, if it has data other than assets one, you need to drop it first. – Arsal Imam Feb 24 '20 at 07:41