简体   繁体   中英

Upgrade Database version in Active Android

I have the following scenario: My app published with database version 2 to the customers. I fixed some bugs and added more features in my DB. This also changed my models and that's why the database changed too. How to give migration scripts in Android studio, I saw suggestion for adding migration scripts for eclipse. But I didn't find any solution for android studio.

Detail Description

I followed the wiki instructions: https://github.com/pardom/ActiveAndroid/wiki/Schema-migrations and tried to add a column to a per-existing db table but that's not working.

My initial enter code here Model class

@Table(name = "Person")
public class Person extends Model 
{
@Column
private String name;
}

All fine. Then I added a new field called age:

@Table(name = "Person")
public class Person extends Model 
{
@Column
private String name;
@Column
private int age;
}

and changed the manifest flag + created the required assets/migrations/2.sql script

ALTER TABLE Person ADD COLUMN age INTEGER;

Where I have to provide this migration script in AndroidStudio environment. Any clues?

I also update the database version in Manifest file. Still i am getting following exception

android.database.sqlite.SQLiteException: no such table: Tablename (code 1): , while compiling: SELECT * FROM TableName
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:891)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:502)
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:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at com.activeandroid.Model.rawQuery(Model.java:349)
at com.activeandroid.Model.rawQuerySingle(Model.java:369)
at com.activeandroid.query.From.executeSingle(From.java:159)
at com.eshopmanager.shopmanager10.MainActivity$1.run(MainActivity.java:127)
at java.util.Timer$TimerImpl.run(Timer.java:284) 

I solved the issue by creating assets folder in main. After that i created migration. Placed sql file in that. So that my database changes effects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM