我在数据库的第一个版本中具有以下模型:
@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();
}
}
现在,我想用新列更新上述模型,因此我用新列更新了模型类,如下所示。
@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();
}
}
我将Active android用于数据库,所以我完成了以下步骤:
1.更新清单文件中的数据库版本号
<meta-data android:name="GC_DB_VERSION" android:value="4" />
2.在资产/迁移中添加了4.sql脚本文件
3.在其中添加了更新查询。
ALTER TABLE chat ADD COLUMN extra_column TEXT;
现在,当我尝试将新的列值插入数据库时,出现以下错误:
android.database.sqlite.SQLiteException:表聊天没有列
命名为extra_column(代码1):,而在编译时:INSERT INTO
聊天(chat_message,Id,extra_column,chat_sender_name,jobId,chatId)
值(?,?,?,?,?,?)
10-16 06:54:29.712 16791-16791 / com.example.manikanta.navigationdrawerpattern
E / SQLiteDatabase:位于
android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
方法)
10-16 06:54:29.712 16791-16791 / com.example.manikanta.navigationdrawerpattern
E / SQLiteDatabase:位于
android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
10-16 06:54:29.712 16791-16791 / com.example.manikanta.navigationdrawerpattern
E / SQLiteDatabase:位于
android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
10-16 06:54:29.712 16791-16791 / com.example.manikanta.navigationdrawerpattern
E / SQLiteDatabase:位于
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)表聊天中没有名为extra_column的列
谁能解释如何解决这个问题?
最佳答案
我已经解决了。
问题原因:
更改清单文件中的版本号时,不会调用活动android的迁移脚本
解决方法:
我已经通过Application.class通过以下行手动设置了数据库版本的更改:
Configuration configuration = new Configuration.Builder(this).setDatabaseVersion(13).create();
ActiveAndroid.initialize(configuration);
我不知道这是否正确,任何更好的方法都值得赞赏。