Code Monkey home page Code Monkey logo

Comments (8)

geovanisouza92 avatar geovanisouza92 commented on September 14, 2024 3

@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();
            }
        }
    }
}

from schematic.

geovanisouza92 avatar geovanisouza92 commented on September 14, 2024

You need to use @OnUpgrade annotated method.

from schematic.

chrisjeon avatar chrisjeon commented on September 14, 2024

@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.

from schematic.

chrisjeon avatar chrisjeon commented on September 14, 2024

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.

from schematic.

geovanisouza92 avatar geovanisouza92 commented on September 14, 2024

DDL/DML is just SQL. :)

from schematic.

chrisjeon avatar chrisjeon commented on September 14, 2024

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?

from schematic.

geovanisouza92 avatar geovanisouza92 commented on September 14, 2024

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.

from schematic.

chrisjeon avatar chrisjeon commented on September 14, 2024

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

from schematic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.