#530: Wrong number of statement arguments with "like '%?%'"

Add "? as a parameter" in the "Good to Know" section to explain how to build like pattern like '%?%'.
This commit is contained in:
Alexandre Colucci committed 2019-05-14 08:59:41 +02:00
1 parent 0e2daff329
commit 490a124cbf
1 file changed
+21
+21
View File
@@ -289,6 +289,7 @@ Documentation
#### Good to Know
- [Avoiding SQL Injection](#avoiding-sql-injection)
- [? as a parameter](#-as-a-parameter)
- [Error Handling](#error-handling)
- [Unicode](#unicode)
- [Memory Management](#memory-management)
@@ -7470,6 +7471,26 @@ try dbQueue.write { db in
```
## ? as a parameter
SQLite only understands `?` as a parameter when it is a placeholder for a whole value (int, double, string, blob, null).
On the other side, the `?` in `'%?%'` is just a character in the `'%?%'` string value: it is not a query parameter, and is not processed in any way.
You need to take this into consideration for queries like:
```sql
SELECT * FROM students where name like '%Robert%';
```
To provide the parameter, you can let SQLite build the like pattern using the string concatenation operator `||`:
```swift
let name = textField.text
try dbQueue.read { db in
try db.execute(sql: "SELECT * FROM students where name like '%' || ? || '%'", arguments: [name])
}
```
## Error Handling
GRDB can throw [DatabaseError](#databaseerror), [PersistenceError](#persistenceerror), or crash your program with a [fatal error](#fatal-errors).