The LIKE operator and Swift

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

The LIKE operator and Swift

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
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: [EXTERNAL] The LIKE operator and Swift

Hick Gunter
You can't have a variable inside a pattern. Use like '%' || ? || '%'

-----Ursprüngliche Nachricht-----
Von: sqlite-users [mailto:[hidden email]] Im Auftrag von Daniel Odom
Gesendet: Donnerstag, 26. September 2019 15:26
An: [hidden email]
Betreff: [EXTERNAL] [sqlite] The LIKE operator and Swift

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
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


___________________________________________
 Gunter Hick | Software Engineer | Scientific Games International GmbH | Klitschgasse 2-4, A-1130 Vienna | FN 157284 a, HG Wien, DVR: 0430013 | (O) +43 1 80100 - 0

May be privileged. May be confidential. Please delete if not the addressee.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: The LIKE operator and Swift

Gwendal Roué-2
In reply to this post by Daniel Odom
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 <[hidden email]> 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
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: The LIKE operator and Swift

J. King-3
In reply to this post by Daniel Odom
On September 26, 2019 9:26:23 a.m. EDT, Daniel Odom <[hidden email]> 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.

<https://sqlite.org/lang_expr.html#like>
--
J. King
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users