iossqlswiftsql-likefmdb

How to using Like with Text in FMDB query iOS Swift 4?


I wanna get query with like and try like this:

let name = "pp" //apple

let db = openDB()
  do {
      var queryString = "select * from products where name like '%?%' and deleted = 0 order by id desc"
      let rs = try db.executeQuery(queryString, values:[name])
      var arr:Array<Product> = []
      while rs.next() {
      arr.append(parseProductDB(rs))
      }
      closeDB(db)
} catch {
      print("failed: \(error.localizedDescription)")
      closeDB(db)
}

but it does not work.


Solution

  • Just try this:

    let db = openDB()
    do {
        var queryString =
        queryString = String(format:"select * from products where name like '%%%@%%' and deleted = 0 order by id desc",name)
        let rs = try db.executeQuery(queryString, values:nil)
        var arr:Array<Product> = []
        while rs.next() {
        arr.append(parseProductDB(rs))
        }
        closeDB(db)
    
    } catch {
                print("failed: \(error.localizedDescription)")
                closeDB(db)
    }