Android SQLite Insert or Update

后端 未结 8 1925
太阳男子
太阳男子 2020-12-04 09:38

as can be seen in the documentation the syntax to make insert or update is : INSERT OR REPLACE INTO

() VALUES (), my
8条回答
  •  -上瘾入骨i
    2020-12-04 10:11

    for me, none of the approaches are work if you don't have "_id" (Primary Key)

    you should first call update, if the affected rows are zero, then insert it with ignore:

     String selection = MessageDetailTable.SMS_ID+" =?";
     String[] selectionArgs =  new String[] { String.valueOf(md.getSmsId())};
    
     int affectedRows = db.update(MessageDetailTable.TABLE_NAME, values, selection,selectionArgs);
    
     if(affectedRows<=0) {
         long id = db.insertWithOnConflict(MessageDetailTable.TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE);
     }
    

提交回复
热议问题