Search code examples
androidandroid-sqliteupdatingsqlite

Update SQLite data base are not reflecting


I developed an application having database in assets folder. The data base copy's from assets folder to device at run time. The code is as follows :

public class DataBaseHelperClass extends SQLiteOpenHelper{
private final Context context;

private static String DB_PATH = "";
// Data Base Name.
private static final String DATABASE_NAME = "db.sqlite";
// Data Base Version.
private static final int DATABASE_VERSION = 1;
// Table Names of Data Base.
static final String TABLE = "MyTable";

static SQLiteDatabase sqliteDataBase;

@SuppressLint("SdCardPath")
public DataBaseHelperClass(Context context) {       
    super(context, DATABASE_NAME, null ,DATABASE_VERSION);
    this.context = context;
    //The Android's default system path of your application database.
    DB_PATH =  "/data/data/com.abc.myapp/databases/";
}

public void createDataBase() throws IOException{
    //check if the database exists
    boolean databaseExist = checkDataBase();

    if(databaseExist){
        this.getWritableDatabase();
    }else{
        this.getReadableDatabase(); 
        copyDataBase(); 
    }// end if else dbExist
} // end createDataBase().

public boolean checkDataBase(){
    File databaseFile = new File(DB_PATH + DATABASE_NAME);
    return databaseFile.exists();        
}

private void copyDataBase() throws IOException{ 
    //Open your local db as the input stream
    InputStream myInput = context.getAssets().open(DATABASE_NAME); 
    // Path to the just created empty db
    String outFileName = DB_PATH + DATABASE_NAME; 
    //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(); 
}

public void openDataBase() throws SQLException{      
    //Open the database
    String myPath = DB_PATH + DATABASE_NAME;
    sqliteDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);  
}

@Override
public synchronized void close() { 
    if(sqliteDataBase != null)
        sqliteDataBase.close(); 
    super.close(); 
}

 @Override
public void onCreate(SQLiteDatabase db) {
    // No need to write the create table query.
    // As we are using Pre built data base.
    // Which is ReadOnly.
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}   

This code works fine. But now i made some changes in data base as :

I created a new table in data base,

Change records and add records in existing table.

And changed the onUpdate of database helper class as :

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if(newVersion > oldVersion){
        //db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
        context.deleteDatabase(DATABASE_NAME);
    }
}

And increase the database version from 1 to 2.

Generated new apk and install in device. It replace the older app and install the new one but the changed made in database are not reflecting.

Now, what should i do to delete data base of older version and copy new data base from assets folder in to device while updating the app.

Please guide me on this issue, as i search on Google on this but not got any working solution to delete and update new data base.


Solution

  • Your oncreate() should be :

    @Override
    public void onCreate(SQLiteDatabase db){
        createDB();
    }
    

    OnUpgrade() Is :

    @Override
    public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
        onCreate(db);
    }
    

    CreateDataBase() is like :

    public void createDataBase(){
        boolean dbExist = dbExists();
        if(!dbExist){
            copyDataBase();
        }
        else if(dbExist){
            copyDataBase();
        }
    }
    

    Then it will work fine.