Android insertWithOnConflict返回错误值
我在Android/SQLite中使用insertWithOnConflict函数来插入数据库中可能已经存在的值。根据Android文档,如果该值存在,将返回主键:Android insertWithOnConflict返回错误值,android,sqlite,insert,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 但是,使
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);
也发生在我身上。我还看到了一些更奇怪的情况,它也返回了0。似乎API级别存在一些问题。回到insertOrThrow,这对我来说一直都很有效 这是一个已知的bug(自2010年以来一直存在):
此API还存在其他问题,例如
我认为最好的解决方案就是避免使用这种API(正如Nazgul所建议的)