草庐IT

insertWithOnConflict

全部标签

Android insertWithOnConflict 返回错误值

我在Android/SQLite中使用insertWithOnConflict函数来插入可能已经存在于数据库中的值。根据Android文档,如果该值存在,将返回主键:ReturnstherowIDofthenewlyinsertedrowORtheprimarykeyoftheexistingrowiftheinputparam'conflictAlgorithm'=CONFLICT_IGNOREOR-1ifanyerror但是,使用该函数时,如果行已经存在,则不会返回现有的主键。相反,它会自动增加主键并返回该值,即使没有数据插入到返回的id的行中。函数调用本身比较简单:name_id

android - 使用 insertWithOnConflict 进行更新或插入

我需要插入或更新,我找到了SQLiteDatabase的insertWithOnConflict方法,但我不知道它如何检查该条目是否已存在。理论上我需要一个“Where”参数来检查某个ID是否存在,如果存在,它应该替换所有其他列。这是我现在拥有的,但我不认为第二个参数是唯一idContentValuesargs=newContentValues();args.put(AppDatabase.COLUMN_ID,entry.getId());args.put(AppDatabase.COLUMN_NAME,entry.getAppname());args.put(AppDatabase.

android - 使用 insertWithOnConflict 进行更新或插入

我需要插入或更新,我找到了SQLiteDatabase的insertWithOnConflict方法,但我不知道它如何检查该条目是否已存在。理论上我需要一个“Where”参数来检查某个ID是否存在,如果存在,它应该替换所有其他列。这是我现在拥有的,但我不认为第二个参数是唯一idContentValuesargs=newContentValues();args.put(AppDatabase.COLUMN_ID,entry.getId());args.put(AppDatabase.COLUMN_NAME,entry.getAppname());args.put(AppDatabase.

android - insertWithOnConflict() 与插入或替换

我正在重构我的代码并将所有insert语句替换为insertorreplace而我发现以下方法使用db.insert()而不是准备好的语句:publicstaticvoidinsertIntoTableByNameAndFieldsAndValues(StringtableName,String[]fields,String[]values,Contextc)throwsFieldsAndValuesMismatchException,UnrecognizedTypeException{DatabaseAbstractionLayerdal=newDatabaseAbstraction

安卓:SQLite - insertWithOnConflict

我正在使用SQLiteDatabase.CONFLICT_IGNORE调用insertWithOnConflict。但是,当发生冲突时,将返回“-1”而不是现有行的ID。我该如何纠正这个问题?创建表:编辑:StringCREATE_CATEGORY_TABLE="CREATETABLE"+TABLE_CATEGORY+"("+BaseColumns._ID+"INTEGERPRIMARYKEYAUTOINCREMENT,"+KEY_CATEGORY_NAME+"TEXTUNIQUE"+")";db.execSQL(CREATE_CATEGORY_TABLE);插入语句:ContentV

android - 为什么 insertWithOnConflict(..., CONFLICT_IGNORE) 返回 -1(错误)?

我有一个SQLite表:CREATETABLEregions(_idINTEGERPRIMARYKEY,nameTEXT,UNIQUE(name));还有一些安卓代码:Validate.notBlank(region);ContentValuescv=newContentValues();cv.put(Columns.REGION_NAME,region);longregionId=db.insertWithOnConflict("regions",null,cv,SQLiteDatabase.CONFLICT_IGNORE);Validate.isTrue(regionId>-1,"