vendredi 16 octobre 2015

Update Existing database table with new column not working in active android

I have the following model in the first version of the database:

@Table(name = DBConstants.TABLE_CHATS)
public class Chat extends Model {

    @Column(name = DBConstants.COLUMN_CHATS_CHAT_ID,unique = true,onUniqueConflict = Column.ConflictAction.REPLACE)
    public String chatId;

    @Column(name = DBConstants.COLUMN_CHATS_CHAT_MESSAGE)
    public String chatMessage;

    @Column(name = DBConstants.COLUMN_CHATS_SENDER_NAME)
    public String chatSenderName;

    @Column(name = DBConstants.COLUMN_JOBS_JOB_ID)
    public String chatJobId;

    public Chat(){super();}

    public Chat(String chatId, String chatMessage, String chatSenderName, String chatJobId){
        super();
        this.chatId = chatId;
        this.chatMessage = chatMessage;
        this.chatSenderName =chatSenderName;
        this.chatJobId = chatJobId;
    }

    public static List<Chat> getChats(String jobId) {
        return new Select()
                .from(Chat.class)
                .where("jobId=?",jobId)
                .execute();
    }
}

Now i want to update the above model with new column, so i have updated the model class with new column as below.

@Table(name = DBConstants.TABLE_CHATS)
public class Chat extends Model {

    @Column(name = DBConstants.COLUMN_CHATS_CHAT_ID,unique = true,onUniqueConflict = Column.ConflictAction.REPLACE)
    public String chatId;

    @Column(name = DBConstants.COLUMN_CHATS_CHAT_MESSAGE)
    public String chatMessage;

    @Column(name = DBConstants.COLUMN_CHATS_SENDER_NAME)
    public String chatSenderName;

    @Column(name = DBConstants.COLUMN_JOBS_JOB_ID)
    public String chatJobId;

    @Column(name = "extra_column")
    public String extraColumn;

    public Chat(){super();}

    public Chat(String chatId, String chatMessage, String chatSenderName, String chatJobId,String extraColumn){
        super();
        this.chatId = chatId;
        this.chatMessage = chatMessage;
        this.chatSenderName =chatSenderName;
        this.chatJobId = chatJobId;
        this.extraColumn = extraColumn;
    }

    public static List<Chat> getChats(String jobId) {
        return new Select()
                .from(Chat.class)
                .where("jobId=?",jobId)
                .execute();
    }
}

I'm using Active android for database so i have done the following steps: 1.Update Database version number in the manifest file

<meta-data android:name="GC_DB_VERSION" android:value="4" />

2.Added 4.sql script file in assets/migrations 3.Added the update query in it.

ALTER TABLE chat ADD COLUMN extra_column TEXT;

  1. Now when i am trying o insert the new column value into database, i'm getting the following error:

    android.database.sqlite.SQLiteException: table chat has no column named extra_column (code 1): , while compiling: INSERT INTO chat(chat_message,Id,extra_column,chat_sender_name,jobId,chatId) VALUES (?,?,?,?,?,?) 10-16 06:54:29.712 16791-16791/com.example.manikanta.navigationdrawerpattern E/SQLiteDatabase: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 10-16 06:54:29.712 16791-16791/com.example.manikanta.navigationdrawerpattern E/SQLiteDatabase: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) 10-16 06:54:29.712 16791-16791/com.example.manikanta.navigationdrawerpattern E/SQLiteDatabase: at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) 10-16 06:54:29.712 16791-16791/com.example.manikanta.navigationdrawerpattern E/SQLiteDatabase: at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 10-16 06:54:29.712 16791-16791/com.example.manikanta.navigationdrawerpattern ....... 10-16 06:54:29.712 16791-16791/com.example.manikanta.navigationdrawerpattern E/SQLiteLog: (1) table chat has no column named extra_column

Can anyone please explain on how to solve this problem?

Aucun commentaire:

Enregistrer un commentaire