【问题标题】:Broken Insert id of new rows新行的插入 id 损坏
【发布时间】:2018-10-06 17:00:26
【问题描述】:

我正在使用 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 行,1-12 和 24 id。

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

【问题讨论】:

    标签: android sqlite sequence android-room


    【解决方案1】:

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

    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();
    

    只是解决问题的方法。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2013-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多