From 0232c8e792fd663996bd709e765578cf04bb0bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 29 May 2019 19:48:33 +0200 Subject: [PATCH] Slight rewriting: focus on what's wrong, and provide a single fix because choices are complicated --- README.md | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 3dd1418ca..d364aa207 100644 --- a/README.md +++ b/README.md @@ -8657,9 +8657,11 @@ let players = try dbQueue.read { db in } ``` -What is important to understand here is that SQLite only interprets `?` as a parameter when it is a placeholder for a whole value (int, double, string, blob, null). In this incorrect query, `?` is just a character in the `'%?%'` string value: it is not a query parameter, and is not processed in any way. See [https://www.sqlite.org/lang_expr.html#varparam](https://www.sqlite.org/lang_expr.html#varparam) for more information about SQLite parameters. +The problem lies in the `'%?%'` pattern. -To provide the parameter, one option is to let Swift build the pattern: +SQLite only interprets `?` as a parameter when it is a placeholder for a whole value (int, double, string, blob, null). In this incorrect query, `?` is just a character in the `'%?%'` string: it is not a query parameter, and is not processed in any way. See [https://www.sqlite.org/lang_expr.html#varparam](https://www.sqlite.org/lang_expr.html#varparam) for more information about SQLite parameters. + +To fix the error, you can feed the request with the pattern itself, instead of the name: ```swift let name = textField.text @@ -8669,25 +8671,6 @@ let players: [Player] = try dbQueue.read { db in } ``` -This solution could also be used with the query interface: - -```swift -let name = textField.text -let players: [Player] = try dbQueue.read { db in - let pattern = "%\(name)%" - return try Player.filter(Column("name").like(pattern)).fetchAll(db) -} -``` - -Another option is to let SQLite build the like pattern using the string concatenation operator `||`: - -```swift -let name = textField.text -let players = try dbQueue.read { db in - try Player.fetchAll(db, sql: "SELECT * FROM player WHERE name LIKE '%' || ? || '%'", arguments: [name]) -} -``` - Sample Code ===========