新行的损坏插入 ID
Broken Insert id of new rows
我正在使用 Room。
我有这个插入方法:
@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 行 1-12 id。
2)我有新数据(例如一行)。启动应用程序并下载+插入数据。
现在 base 包含 13 行,id 为 1-12 和 24。
我不知道有什么问题,请帮助我
我认为问题与 monthIndex 属性的唯一索引有关。请检查您如何设置此属性的值。检查您的数据库关联列的值。
您可以通过以下代码插入新元素:
MonthDao.insert(new Month(monthDao.getLastMonthId()+1, monthTitle, monthUrl, mIndex));
...
@Query("SELECT * FROM news_month_table ORDER BY month_id DESC LIMIT 1;")
int getLastMonthId();
就是解决问题的方法。
我正在使用 Room。 我有这个插入方法:
@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 行 1-12 id。 2)我有新数据(例如一行)。启动应用程序并下载+插入数据。 现在 base 包含 13 行,id 为 1-12 和 24。
我不知道有什么问题,请帮助我
我认为问题与 monthIndex 属性的唯一索引有关。请检查您如何设置此属性的值。检查您的数据库关联列的值。
您可以通过以下代码插入新元素:
MonthDao.insert(new Month(monthDao.getLastMonthId()+1, monthTitle, monthUrl, mIndex));
...
@Query("SELECT * FROM news_month_table ORDER BY month_id DESC LIMIT 1;")
int getLastMonthId();
就是解决问题的方法。