Android insertWithOnConflict返回错误的值

时间:2011-12-29 03:35:38

标签: android sqlite insert

我在Android / SQLite中使用insertWithOnConflict函数来插入可能已存在于数据库中的值。根据Android文档,如果值存在,将返回主键:

Returns
the row ID of the newly inserted row OR the primary key of the existing row if 
the input param 'conflictAlgorithm' = CONFLICT_IGNORE OR -1 if any error 

但是,使用此函数时,如果该行已存在,则不会返回现有主键。相反,它会自动增加主键并返回该值,即使没有数据插入到返回的id的行中。

函数调用本身相对简单:

name_id = db.insertWithOnConflict("names", null, name_values, SQLiteDatabase.CONFLICT_IGNORE);

2 个答案:

答案 0 :(得分:1)

也发生在我身上。我还看到了一些更奇怪的案例,它也归还了我0。似乎API级别存在一些问题。已经恢复为insertOrThrow,那对我来说是有用的。

答案 1 :(得分:0)

这是一个已知的错误(自2010年以来):
https://code.google.com/p/android/issues/detail?id=13045

此API存在其他问题,例如 https://code.google.com/p/android/issues/detail?id=3302

我认为最好的解决方案就是避免这种API(正如Nazgul建议的那样)。