Android 新行的插入id已损坏

Android 新行的插入id已损坏,android,sqlite,sequence,android-room,Android,Sqlite,Sequence,Android Room,我在用房间。 我有这个插入方法: @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(Month month); for(...){ ... monthDao.insert(new Month(0, monthTitle, monthUrl)); } 我的插入方法: @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(Month month); for

我在用房间。 我有这个插入方法:

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Month month);
for(...){
...
monthDao.insert(new Month(0, monthTitle, monthUrl));
}
我的插入方法:

@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Month month);
for(...){
...
monthDao.insert(new Month(0, monthTitle, monthUrl));
}
也是我的模特

@Entity(tableName = "news_month_table", indices = @Index(value = {"month_index"}, unique = true))
public class Month {

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "month_id")
@Getter @Setter
public long month_id;
@ColumnInfo(name = "month_name")
@Getter @Setter
public String monthName;
@ColumnInfo(name = "month_url")
@Getter @Setter
public String monthURL;
@ColumnInfo(name = "month_index")
@Getter @Setter
public int monthIndex;
我的情况: 1) 启动应用程序并下载+插入数据(12轮for()) 现在base是正确的,它包含12行,id为1-12。 2) 我有新数据(例如一行)。启动应用程序并下载并插入数据。 现在,base包含13行,id为1-12和24


我不知道是什么问题,请帮助我

我认为问题与monthIndex属性上的唯一索引有关。请检查如何设置此属性的值。在数据库中检查关联列的值。

您可以通过以下代码插入新元素:

MonthDao.insert(new Month(monthDao.getLastMonthId()+1, monthTitle, monthUrl, mIndex));

这就是解决问题的方法