Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FAQ to clarify "Wrong number of statement arguments" error with "like '%?%'" #538

Merged
merged 9 commits into from
May 29, 2019
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ Documentation
#### Good to Know

- [Avoiding SQL Injection](#avoiding-sql-injection)
- [? as a parameter](#-as-a-parameter)
Timac marked this conversation as resolved.
Show resolved Hide resolved
- [Error Handling](#error-handling)
- [Unicode](#unicode)
- [Memory Management](#memory-management)
Expand Down Expand Up @@ -8506,6 +8507,7 @@ FAQ
- [Generic parameter 'T' could not be inferred](#generic-parameter-t-could-not-be-inferred)
- [SQLite error 10 "disk I/O error", SQLite error 23 "not authorized"](#sqlite-error-10-disk-io-error-sqlite-error-23-not-authorized)
- [What Are Experimental Features?](#what-are-experimental-features)
- [SQLite error 21 "wrong number of statement arguments" with LIKE queries](#sqlite-error-21-wrong-number-of-statement-arguments-with-like-queries)
Timac marked this conversation as resolved.
Show resolved Hide resolved


### How do I create a database in my application?
Expand Down Expand Up @@ -8645,6 +8647,29 @@ There is an exception, though: *experimental features*, marked with the "**:fire
Those experimental features are not protected by semantic versioning, and may break between two minor releases of the library. To help them becoming stable, [your feedback](https://github.com/groue/GRDB.swift/issues) is greatly appreciated.


### SQLite error 21 "wrong number of statement arguments" with LIKE queries

You may get the error "wrong number of statement arguments" when executing a LIKE query similar to:

```swift
let name = textField.text
try dbQueue.read { db in
try db.execute(sql: "SELECT * FROM students where title like '%?%'", arguments: [name])
Timac marked this conversation as resolved.
Show resolved Hide resolved
}
```

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.

To provide the parameter, one option is to let SQLite build the like pattern using the string concatenation operator `||`:
Timac marked this conversation as resolved.
Show resolved Hide resolved

```swift
let name = textField.text
try dbQueue.read { db in
try db.execute(sql: "SELECT * FROM students where name like '%' || ? || '%'", arguments: [name])
}
```


Sample Code
===========

Expand Down