4

I am using Active Android in order to perform sqllite operations in my android app. Following is a model class.

@Table(name="books")
public class Books extends Model {
@Column(name = "bookId")
public String bookId;
@Column(name = "bookName")
public String bookName;
}

I want to add a new column to this. So I followed the instructions on Active Android's Schema Migration Doc.

a. Updated the AA_DB_VERSION

b. Added a new sql file in assets/migrations

Then when I run my app I get the below exception

1553-1553/com.hannan.delivery E/SQLiteLog﹕ (21) API called with NULL prepared statement
1553-1553/com.hannan.delivery E/SQLiteLog﹕ (21) misuse at line 63241 of [00bb9c9ce4]
java.lang.RuntimeException: Unable to create application com.hannan.delivery.MyApplication: android.database.sqlite.SQLiteException: not an error (code 0)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4154)
        at android.app.ActivityThread.access$1300(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
        ....
 Caused by: android.database.sqlite.SQLiteException: not an error (code 0)
        at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
        at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:727)
        at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
        at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1665)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
        at com.activeandroid.DatabaseHelper.executeSqlScript(DatabaseHelper.java:180)
        at com.activeandroid.DatabaseHelper.executeMigrations(DatabaseHelper.java:150)
        at com.activeandroid.DatabaseHelper.onUpgrade(DatabaseHelper.java:74)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
        at com.activeandroid.Cache.openDatabase(Cache.java:102)
        at com.activeandroid.Cache.initialize(Cache.java:75)
        at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:44)
        at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:34)
        at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:30)
        at com.activeandroid.app.Application.onCreate(Application.java:25)
        at com.hannan.delivery.MyApplication.onCreate(MyApplication.java:51)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
        ...

Here's MyApplication's Create method where it is failing.

@Override
public void onCreate() {
    super.onCreate();
    ActiveAndroid.initialize(this);
}

Migration Script is: (2.sql)

ALTER TABLE books ADD COLUMN bookAuthor TEXT;

Kindly suggest as to why I am getting this error.

Hannan Shaik
  • 1,298
  • 2
  • 14
  • 27

3 Answers3

1

I was running into issues with this and realized that if you have run the application ONCE with the upgrade sql file in the wrong location, the database has already been "upgraded" and any subsequent attempts to get it working will be ignored (since the database version is already incremented). You can see if this is the case by lowering the database version back to the original value, you will get an error on startup saying it cannot be downgraded.

Bumping the version number up to 3 and renaming the sql file to 3.sql made it run correctly.

Not sure if this will be the case if something is wrong inside the sql file when its run as well, just thought it might be useful to someone while troubleshooting.

0

Try this to add new column to sqlite table

ALTER TABLE books ADD bookAuthor TEXT ;

This query should come in

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

cause oncreate fires only when app is run for the first time & onupgrade fires on version change . So just change version & put this in onUpgrade and try.

Gautami
  • 361
  • 1
  • 6
0

This answer might help you:

https://stackoverflow.com/a/27222137/858418

It's possible you just need to make sure your migration file starts with 0.sql.

In addition, make sure that your 0.sql is located under app/src/main/assets/migrations.

Community
  • 1
  • 1
Marina
  • 3,222
  • 5
  • 25
  • 35