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

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

Closed
Timac opened this issue May 13, 2019 · 4 comments
Closed

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

Timac opened this issue May 13, 2019 · 4 comments
Labels

Comments

@Timac
Copy link
Contributor

Timac commented May 13, 2019

What did you do?

I am trying to perform some SQLite queries using the like operator.
For example I would like to delete all rows from mytable where the title column contains the string "something":

do {
	try databasePool.write { db in
		try db.execute(sql: "DELETE FROM mytable where title like '%?%'", arguments: ["something"])
	}
} catch let error as DatabaseError {
	debugPrint("Failed with error \(error)")
} catch {
	debugPrint("Failed with unknown error")
}

What did you expect to happen?

The SQLite query looks valid. I would expect the query to delete all rows from mytable where the substring is found.

What happened instead?

I see instead an error SQLite error 21: wrong number of statement arguments: 1
Note that if I use instead the following, I don't see the error and the expected rows are deleted:

try db.execute(sql: "DELETE FROM mytable where title like '%\(something)%'")

A similar issue occurs when using SELECT:

try db.execute(sql: "SELECT * FROM mytable where title like '%?%'", arguments: ["something"])

Environment

GRDB flavor(s): GRDB
GRDB version: 4.0 (revision 0e2daff)
Installation method: manual
Xcode version: 10.2.1
Swift version: 5
Platform(s) running GRDB: iOS
macOS version running Xcode: 10.14.4

@groue
Copy link
Owner

groue commented May 13, 2019

Please see Nike-Inc/SQift#9 for an identical issue. It is a misunderstanding of what SQLite parameters are.

@Timac
Copy link
Contributor Author

Timac commented May 14, 2019

@groue Thanks a lot for the clarification!
Since I am not the first one asking this question and probably not the last one, I added a paragraph to the README to clarify this point.

See #531

@Timac Timac closed this as completed May 14, 2019
@groue
Copy link
Owner

groue commented May 14, 2019

That's lovely, @Timac! You're right, this is a FAQ :-) I'll soon look into your PR.

@sobri909
Copy link
Collaborator

It also works if you put the percent marks in the argument.

let notes = Stalker.store.notes(
    where: "body LIKE ? AND deleted = 0 ORDER BY date DESC",
    arguments: ["%\(query)%"])

@groue groue added the support label May 15, 2019
groue added a commit that referenced this issue May 29, 2019
Add FAQ to clarify "Wrong number of statement arguments" error with "like '%?%'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants