我试图一次插入多行。
表格如下所示,
e.index = [str(n)+'mean' for n in range(1,13)]
df = df.append(e)
df = df.sort_index()
并插入功能
@Entity(nameInDb = "options")
public class Option {
@Expose
@SerializedName("id")
@Id(autoincrement = true)
private Long id;
@Expose
@SerializedName("option_text")
@Property(nameInDb = "option_text")
private String optionText;
@Expose
@SerializedName("question_id")
@Property(nameInDb = "question_id")
private Long questionId;
@Expose
@SerializedName("is_correct")
@Property(nameInDb = "is_correct")
private boolean isCorrect;
@Expose
@SerializedName("created_at")
@Property(nameInDb = "created_at")
private String createdAt;
@Expose
@SerializedName("updated_at")
@Property(nameInDb = "updated_at")
private String updatedAt;
@Generated(hash = 435722130)
public Option(Long id, String optionText, Long questionId, boolean isCorrect,
String createdAt, String updatedAt) {
this.id = id;
this.optionText = optionText;
this.questionId = questionId;
this.isCorrect = isCorrect;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
@Generated(hash = 104107376)
public Option() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getOptionText() {
return optionText;
}
public void setOptionText(String optionText) {
this.optionText = optionText;
}
public Long getQuestionId() {
return questionId;
}
public void setQuestionId(Long questionId) {
this.questionId = questionId;
}
public boolean isCorrect() {
return isCorrect;
}
public void setCorrect(boolean correct) {
isCorrect = correct;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
public boolean getIsCorrect() {
return this.isCorrect;
}
public void setIsCorrect(boolean isCorrect) {
this.isCorrect = isCorrect;
}
}
此处选项列表包含除" id"之外的所有字段。这会自动增加。所以我在id的位置传递null。
但是我得到了唯一约束失败的异常。如果我只传递一个项目,则插入成功。
如何插入多行。
答案 0 :(得分:0)
使您的Option类如下面的代码。
@Entity(
indexes = {
@Index(value = "id", unique = true)
}
)
public class Option {
@Id(autoincrement = true)
private Long id;
@Property(nameInDb = "option_text")
private String optionText;
@Property(nameInDb = "question_id")
private Long questionId;
@Property(nameInDb = "is_correct")
private boolean isCorrect;
@Property(nameInDb = "created_at")
private String createdAt;
@Property(nameInDb = "updated_at")
private String updatedAt;
public Option(String optionText, Long questionId, boolean isCorrect,
String createdAt, String updatedAt) {
this.optionText = optionText;
this.questionId = questionId;
this.isCorrect = isCorrect;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
public Option() {
}
@Generated(hash = 435722130)
public Option(Long id, String optionText, Long questionId, boolean isCorrect,
String createdAt, String updatedAt) {
this.id = id;
this.optionText = optionText;
this.questionId = questionId;
this.isCorrect = isCorrect;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
public String getOptionText() {
return optionText;
}
public void setOptionText(String optionText) {
this.optionText = optionText;
}
public Long getQuestionId() {
return questionId;
}
public void setQuestionId(Long questionId) {
this.questionId = questionId;
}
public boolean isCorrect() {
return isCorrect;
}
public void setCorrect(boolean correct) {
isCorrect = correct;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
public boolean getIsCorrect() {
return this.isCorrect;
}
public void setIsCorrect(boolean isCorrect) {
this.isCorrect = isCorrect;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
}
并插入如下数据。
mDaoSession.getOptionDao().insert(new Option("anyText", 3L, true, "yesterday", "today"));
ID会自动递增。
这将解决您的问题。
答案 1 :(得分:0)
我发现我正在插入具有相同数据的对象列表,我的意思是说我插入了冗余对象,因此它抛出了错误。我们无法插入冗余数据,因此问题得以解决。