1

I am learning Android. I have one Quote application with SQLite database. I need to give Database version to this application so Whenever I update application, users database also can be get updated. What changes I need to made in this ?

My database Helper/ Handler Class is like below.

public class DataBaseHandler extends SQLiteOpenHelper {
    private static String DB_PATH;
    private static String DB_NAME = "SuccessQuotesNew";
    private SQLiteDatabase myDataBase;
    private final Context myContext;
    public DataBaseHandler(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
        DB_PATH = context.getDatabasePath(DB_NAME).toString();
        Log.e("path", DB_PATH);
    }

    public void createDataBase() throws IOException {

        boolean dbExist = checkDataBase();

        if (dbExist) {

        } else {
            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }

    // ==============================================================================

    private boolean checkDataBase() {

        SQLiteDatabase checkDB = null;

        try {
            String myPath = DB_PATH;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        } catch (SQLiteException e) {

        }

        if (checkDB != null) {

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    private void copyDataBase() throws IOException {

        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH;
        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();

    }

    // ==============================================================================

    public void openDataBase() throws SQLException {

        // Open the database
        String myPath = DB_PATH;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    // ==============================================================================

    @Override
    public synchronized void close() {

        if (myDataBase != null)
            myDataBase.close();

        super.close();

    }

    // ==============================================================================

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    // ==============================================================================

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

    }


}
0

1 Answer 1

0

last parameter of constructor is version code for database. super(context, DB_NAME, null, 1); change that number and check update method will gets called so when ever you want to update db change it and app will call update method once you create object of that.

For reference check here - Android SQLite database and app update

Sign up to request clarification or add additional context in comments.

3 Comments

hi ! Thanks for suggestion....and What should I put in condition for upgrade method as per my code located in question for make application working smoothly ?
@RajubhaiRathod paste your code from where you are creating database object. i will update it.
sorry but I do not understood what you are asking me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.