sqlite 使用Android 5.0 API进行SqlLite查询时出错21

o75abkj4  于 2022-12-13  发布在  SQLite
关注(0)|答案(1)|浏览(143)

Android 5.0 API 21会出现此错误,但此代码可与Android API 33完美配合

我正在使用SimpleSQLiteQuery创建我的选择查询[![在此处输入图像描述

override fun getPropertyBySearch(
    type: String,
    city: String,
    minSurface: Int,
    maxSurface: Int,
    minPrice: Int,
    maxPrice: Int,
    onTheMarketLessALastWeek: Boolean,
    soldOn3LastMonth: Boolean,
    min3photos: Boolean,
    schools: Boolean,
    shops: Boolean
): LiveData<List<RealEstateDatabase>> {

    val iso = ISOChronology.getInstance()
    val today = LocalDate(iso)

    Log.e("today",today.toString())

    val dateMinusThreeMonth = today.minusMonths(3)
    val dateMinus1Week = today.minusDays(7)

    Log.e("dateMinusThreeMonth",dateMinusThreeMonth.toString())
    Log.e("dateMinus1Week",dateMinus1Week.toString())

    val query = """SELECT * FROM RealEstateDatabase WHERE 
                    ('$type' ='' OR type LIKE '%$type%' ) AND 
                    ('$city' ='' OR city LIKE '%$city%' ) AND
                    ($schools = false OR schoolsNear = $schools ) AND 
                    ($shops = false OR shopsNear = $shops ) AND 
                    ($min3photos = false OR count_photo >= 3 ) AND
                    ($minSurface =0 AND $maxSurface = 0  OR  area BETWEEN $minSurface AND $maxSurface  ) AND 
                    ($minPrice =0 AND $maxPrice = 0  OR  price BETWEEN $minPrice AND $maxPrice ) AND 
                    ($onTheMarketLessALastWeek = false  OR  dateOfEntry BETWEEN '$dateMinus1Week' AND '$today' ) AND 
                    ($soldOn3LastMonth = false  OR dateOfSale BETWEEN '$dateMinusThreeMonth' AND '$today') """

    Log.e("query", query)

    return realEstateDao.getPropertyBySearch(SimpleSQLiteQuery(query))
}

这就是我们要讨论的table

@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,
)

我没有解释这段代码在一个API上工作,而不是另一个,我倾向于我的sql lite查询的不兼容性,但我不确定或如何修复它

woobm2wo

woobm2wo1#

常量false(以及true)在SQLite 3.23.0版本中引入;依据:-

  1. 2018年4月2日(3月23日)
  2. 1.使用SQLITE_ENABLE_DESERIALIZE编译时选项时,添加sqlite3_serialize()与sqlite3_deserialize()接口.
  3. 1.将TRUE和FALSE识别为常数。(为了相容性,如果存在名为“true”或“false”的数据行,则识别码会指涉数据行,而不是布林常数。)
    1.支持运算符IS TRUE、IS FALSE、IS NOT TRUE和IS NOT FALSE。
  4. https://www.sqlite.org/changes.html
    因此,根据,只有30+的android API支持使用true和false

相反,您可以使用0来代替false(非零,通常1表示true)。例如:

val query = """SELECT * FROM RealEstateDatabase WHERE 
                    ('$type' ='' OR type LIKE '%$type%' ) AND 
                    ('$city' ='' OR city LIKE '%$city%' ) AND
                    ($schools = 0 OR schoolsNear = $schools ) AND 
                    ($shops = 0 OR shopsNear = $shops ) AND 
                    ($min3photos = 0 OR count_photo >= 3 ) AND
                    ($minSurface =0 AND $maxSurface = 0  OR  area BETWEEN $minSurface AND $maxSurface  ) AND 
                    ($minPrice =0 AND $maxPrice = 0  OR  price BETWEEN $minPrice AND $maxPrice ) AND 
                    ($onTheMarketLessALastWeek = 0  OR  dateOfEntry BETWEEN '$dateMinus1Week' AND '$today' ) AND 
                    ($soldOn3LastMonth = 0  OR dateOfSale BETWEEN '$dateMinusThreeMonth' AND '$today') """

相关问题