在SQLITE中使用独立条件更新多行

u3r8eeie  于 2022-11-30  发布在  SQLite
关注(0)|答案(1)|浏览(126)
@Query("UPDATE  RealEstateDatabase SET type = :entryType   WHERE id = :id AND type NOT LIKE :entryType")
suspend fun updateRealEstate(entryType: String, id: String)

此代码可以根据条件完美地更新表中的行。我想对每个链接到条件的多个行执行相同的操作
此处是相关实体/表

@Entity
@Parcelize
data class RealEstateDatabase(
@PrimaryKey
var id: String,
var type: String? = null,
var price: Int? = null,
var area: Int? = null,
var numberRoom: String? = null,
var description: String? = null,
var numberAndStreet: String? = null,
var numberApartment: String? = null,
var city: String? = null,
var region: String? = null,
var postalCode: String? = null,
var country: String? = null,
var status: String? = null,
var dateOfEntry: String? = null,
var dateOfSale: String? = null,
var realEstateAgent: String? = null,
var lat: Double ?=null,
var lng: Double ?=null,
var hospitalsNear : Boolean = false,
var schoolsNear : Boolean = false,
var shopsNear : Boolean = false,
var parksNear : Boolean = false,
@ColumnInfo(name = "listPhotoWithText")
var listPhotoWithText : List<PhotoWithTextFirebase> ?=null,
var count_photo : Int? = listPhotoWithText?.size,
)

我也把我的存储库的方法,这个方法有与我的表相同类型的参数来修改

override suspend fun updateRealEstate(
    id: String,
    entryType: String,
    entryPrice: String,
    entryArea: String,
    entryNumberRoom: String,
    entryDescription: String,
    entryNumberAndStreet: String,
    entryNumberApartement: String,
    entryCity: String,
    entryRegion: String,
    entryPostalCode: String,
    entryCountry: String,
    entryStatus: String,
    textDateOfEntry: String,
    textDateOfSale: String,
    realEstateAgent: String?,
    lat: Double?,
    lng: Double?,
    checkedStateHopital: MutableState<Boolean>,
    checkedStateSchool: MutableState<Boolean>,
    checkedStateShops: MutableState<Boolean>,
    checkedStateParks: MutableState<Boolean>,
    listPhotoWithText: List<PhotoWithTextFirebase>?,
    itemRealEstate: RealEstateDatabase
): Response<Boolean> {
    return try {
        Response.Loading

        val rEcollection = firebaseFirestore.collection("real_estates")

        if(entryType != itemRealEstate.type ){
            rEcollection.document(id).update("type",entryType)
        }
        
        realEstateDao.updateRealEstate(entryType,id)

        Response.Success(true)
    }catch (e: Exception) {
        Response.Failure(e)
    }
}

我重复一遍,我想更新表中的行,条件是由我的repo的方法给出的变量与所讨论的行不同

@Query("UPDATE  RealEstateDatabase SET " +
        "type = (CASE WHEN type NOT LIKE :entryType THEN (:entryType) ELSE type END) ," +
        "price = (CASE WHEN price NOT LIKE :entryPrice THEN (:entryPrice) ELSE price END) WHERE id =:id")
suspend fun updateRealEstate(
    entryType: String,
    id: String,
    entryPrice: Int
)

此解决方案有效,但仍强制我更新值

6mzjoqzu

6mzjoqzu1#

您可以在WHERE子句中添加一个条件,如果两个不**需要更新,该条件将阻止UPDATE语句的执行:

@Query("UPDATE  RealEstateDatabase SET " +
       "type = CASE WHEN type NOT LIKE :entryType THEN (:entryType) ELSE type END," +
       "price = CASE WHEN price NOT LIKE :entryPrice THEN (:entryPrice) ELSE price END " +
       "WHERE id = :id AND (type NOT LIKE :entryType OR price NOT LIKE :entryPrice)")

相关问题