Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do we make changes to the database #44

Closed
chrisjeon opened this issue Jan 5, 2016 · 8 comments
Closed

How do we make changes to the database #44

chrisjeon opened this issue Jan 5, 2016 · 8 comments

Comments

@chrisjeon
Copy link

Hi,

I'm wondering how we can make changes to our database (like add/remove columns to the tables, add additional tables, and etc.)

It doesn't seem to be as simple as bumping up the DATABASE_VERSION number. Doing that seems to "upgrade" the database, but it doesn't make any changes to the actual database schema itself.

The github repo seems to be lacking documentation on this, and I couldn't find much on Google either. Can anyone help me out? The more I learn, I'm willing to contribute to make the documentation more solid.

@geovanisouza92
Copy link

You need to use @OnUpgrade annotated method.

@chrisjeon
Copy link
Author

@geovanisouza92 can you give an example on how we use that? Do we just declare it like that in the link? Because I tried it, and it doesn't work. It seems that we have to fill in the method some how.

@geovanisouza92
Copy link

@chrisjeon Try something like this

@Database(fileName = MyDatabase.FILE_NAME, version = MyDatabase.VERSION)
public final class MyDatabase {

    public static final int VERSION = 1;
    public static final String FILE_NAME = "my.sqlite";

    public static final String[] _MIGRATIONS = {
        // Put DDL/DML commands here, one string per VERSION increment
    };

    @OnUpgrade
    public static void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        for (int i = oldVersion; i < newVersion; i++) {
            String migration = _MIGRATIONS[i - 2];
            db.beginTransaction();
            try {
                db.execSQL(migration);
                db.setTransactionSuccessful();
            } catch (Exception e) {
                Timber.e(e, "Error executing database migration: %s", migration);
                break;
            } finally {
                db.endTransaction();
            }
        }
    }
}

@chrisjeon
Copy link
Author

In the comment inside _MIGRATIONS, what is "Put DDL/DML commands here, one string per VERSION increment" mean? Sorry, I'm pretty new to Android.

@geovanisouza92
Copy link

DDL/DML is just SQL. :)

@chrisjeon
Copy link
Author

Ah, so let's say I have 3 sql statements that I would like to run, I would have to bump up the version number by 3?

@geovanisouza92
Copy link

You could use DML (CREATE TABLE, ALTER TABLE) or DML (INSERT, UPDATE, DELETE) to modify your db.

Upgrade to 4, because 1 is the first version, and you are applying 3 statements. With the example code that I put above, if you set VERSION to 3, the last command will not be applied.

@chrisjeon
Copy link
Author

Thanks dude, when I get the time, I'll do a PR of updated README.md with better documentation.

@SimonVT SimonVT closed this as completed Feb 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants