Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can ORMLite auto-upgrade a database by dropping and recreating?

We have an ORMLite solution implemented in our current application. We use it for caching results from REST calls from our servers. So, it's not super important that the data be there (or be left over when a user updates the app). We just added an update which created a new column in one of our database-persisted models, Shift, and we're now seeing the following exception:

Caused by: android.database.sqlite.SQLiteException: no such column: acknowledged (code 1): , while compiling: SELECT * FROM ...
       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(SQLiteConnection.java)
       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
       at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
       at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
       at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
       at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1430)
       at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1369)
       at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:162)
       at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:57)
       at com.j256.ormlite.stmt.SelectIterator.<init>(SelectIterator.java:55)
       at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:232)
       at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:181)
       at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:263)

I checked to make sure this field was added to the ORMLite configuration file, and it was, so I'm not sure what's happening here. I thought that ORMLite will drop the database and re-create it if it finds that the schema has changed (and you don't add an onUpgrade() method to the DatabaseHelper), but I'm now thinking I'm incorrect.

How do I fix this? I've tried reading through the ORMLite manual, but either I glossed over the section about default upgrade behavior or I didn't find it...

like image 239
jwir3 Avatar asked Feb 23 '26 12:02

jwir3


1 Answers

I thought that ORMLite will drop the database and re-create it if it finds that the schema has changed (and you don't add an onUpgrade() method to the DatabaseHelper), but I'm now thinking I'm incorrect.

You are incorrect. ORMLite does not have any magic code that handles schema updating. Check out the docs for upgrading your schema from the manual. To quote:

When you upgrade your application, you may have to add columns or make other changes to the data that was stored by previous versions of your application. If you are on Android then in your DatabaseHelper, there should be an onUpgrade() method that extends the following method from the OrmLiteSqliteOpenHelper.

abstract void onUpgrade(SQLiteDatabase database,
ConnectionSource connectionSource, int oldVersion, int newVersion)

In that method you can use your DAO to perform any tweaks to the schema:

Dao<Account, Integer> dao = getHelper().getAccountDao();
// change the table to add a new column named "age" 
dao.executeRaw("ALTER TABLE `account` ADD COLUMN age INTEGER;");

So you will need to do the upgrading yourself. The docs also show how you can use the database version numbers to make specific changes based on which database version your client has.

If you don't care about the existing tables then the onUpgrade(...) method can just drop the tables and call onCreate(...).

like image 164
Gray Avatar answered Feb 26 '26 03:02

Gray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!