Re: [sqlite] The LIKE operator and Swift

2019-09-27 Thread J. King
On September 26, 2019 9:26:23 a.m. EDT, Daniel Odom  wrote:
>I am just now getting around to learning Swift and XCode. I am having a
>
>problem with 'LIKE'. When I do this:
>
>let queryString = "select name, phone, street, city, state from phone 
>where name like '%?%'"

As others have said, '?' is a literal character, whereas ? is a parameter. 
Concatenating with || is one solution, though I would personally recommend 
instead preparing your whole pattern in your application logic and passing just 
the parameter to SQLite. Don't forget to escape any literal % and _ characters 
in your input before fencing with %, unless your input is itself a LIKE 
pattern. 


-- 
J. King
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] The LIKE operator and Swift

2019-09-27 Thread Gwendal Roué
You can build your pattern in Swift, and use a single parameter:

let queryString = "select name, phone, street, city, state from phone
where name like '?'"
let pattern = "%\(name)%"
if sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_TRANSIENT) !=
SQLITE_OK {do whatever}

For a more detailed explanation of the reasons why you get an error with
this LIKE query see this dedicated FAQ:
https://github.com/groue/GRDB.swift/blob/master/README.md#sqlite-error-21-wrong-number-of-statement-arguments-with-like-queries

Gwendal Roué

On Thu, Sep 26, 2019 at 3:26 PM Daniel Odom  wrote:

> I am just now getting around to learning Swift and XCode. I am having a
> problem with 'LIKE'. When I do this:
>
> let queryString = "select name, phone, street, city, state from phone
> where name like '%?%'"
>
> And then this: if sqlite3_bind_text(stmt, 1, name, -1, SQLITE_TRANSIENT)
> != SQLITE_OK {do whatever}
>
> I get an error "column index out of range". The rest of the code is
> fine. When I do this:
>
> let queryString = "select name, phone, street, city, state from phone
> where name = ?"
>
> everything works just fine. What am I missing?
>
> ___
> sqlite-users mailing list
> sqlite-users@mailinglists.sqlite.org
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] The LIKE operator and Swift

2019-09-26 Thread Daniel Odom
I am just now getting around to learning Swift and XCode. I am having a 
problem with 'LIKE'. When I do this:


let queryString = "select name, phone, street, city, state from phone 
where name like '%?%'"


And then this: if sqlite3_bind_text(stmt, 1, name, -1, SQLITE_TRANSIENT) 
!= SQLITE_OK {do whatever}


I get an error "column index out of range". The rest of the code is 
fine. When I do this:


let queryString = "select name, phone, street, city, state from phone 
where name = ?"


everything works just fine. What am I missing?

___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users