gpt4 book ai didi

android - 使用新列更新现有数据库表在 Activity android 中不起作用

转载 作者:行者123 更新时间:2023-11-29 20:21:16 25 4
gpt4 key购买 nike

我在第一个版本的数据库中有以下模型:

@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.更新 list 文件中的数据库版本号

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

2.assets/migrations新增4.sql脚本文件

3.在其中添加更新查询。

ALTER TABLE chat ADD COLUMN extra_column TEXT;
  1. 现在,当我尝试将新列值插入数据库时​​,出现以下错误:

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

谁能解释一下如何解决这个问题?

最佳答案

我已经解决了。

问题原因:

Activity android 的迁移脚本不会在更改 list 文件中的版本号时被调用

解决方法:

我已经通过 Application.class 使用以下行手动设置数据库版本更改:

Configuration configuration = new Configuration.Builder(this).setDatabaseVersion(13).create();

ActiveAndroid.initialize(configuration);

我不知道这是否是正确的做法,欢迎任何更好的方法。

关于android - 使用新列更新现有数据库表在 Activity android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33164456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com