androidsqliteandroid-sqlitesugarorm

How to create another table in database with SugarORM


I have an application and I'm using Sugar ORM library v1.5 to create database. I released first version of app and now I want to add another table to database, I installed new version on previous version but it crashes. This is the error I've got. How can I solve it? Should I increase the version of database?

Thanks in advance

android.database.sqlite.SQLiteException: no such table: CUSTOMER_SERVICE_REPORT (code 1): , while compiling: INSERT OR REPLACE  INTO CUSTOMER_SERVICE_REPORT(SERIAL_NUMBER,COSTUMER_NAME,DATE,DESCRIPTION,FACTOR,SERVICE_ID,ID,CHECK_LIST_NUMBER,REMINDER) VALUES (?,?,?,?,?,?,?,?,?)
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1546)
    at com.orm.SugarRecord.save(SugarRecord.java:280)
    at com.orm.SugarRecord.save(SugarRecord.java:416)

Solution

  • Expanding on @Prasanth S's comment, just adding new tables only requires incrementing the VERSION, so Sugar ORM knows to look for new tables.

    However, if you wish to make other data changes, you'll need to set up a migration script. This can be done by adding <version>.sql in /assets/sugar_upgrades/. For example, adding 3.sql will cause this file to be run when migrating to version 3.

    The SQL file just contains a list of SQL commands to run on your local SQLite database.

    More info is in the official documentation.