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 documentation to clarify #530 Wrong number of statement arguments with "like '%?%'" #531

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 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)
- [Error Handling](#error-handling)
- [Unicode](#unicode)
- [Memory Management](#memory-management)
Expand Down Expand Up @@ -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).
Expand Down