As you've found, merely using the same text as a variable name and as a substring of a string literal does not cause the contents of the variable to be spliced into the string. The compiler could theoretically set this up for you, but it doesn't—doing this with no explicit formatting characters isn't a feature of any language I've ever heard of. Even if this were supported, you still couldn't splice NSString objects into C strings.
So, the question is how to splice the string from str
into the query string.
The answer is not stringWithFormat:
!
That opens you up to SQL injection, which, in the context of a database that the user is accessing from the same machine where it resides, means that the user could corrupt their data by entering the wrong thing. Trying to fix that by escaping things is an interminable game of whack-a-mole and self-doubt (“I hope that's the last of these bugs”).
The correct solution is to let SQLite splice the parameters into the query as only it knows how. You do that by preparing the statement with a question mark at every point where you need to splice in a value, then binding the value(s) into the statement. Each call to one of the bind functions will replace one not-yet-bound placeholder with the value you pass in in a way that is safe—i.e., won't create an invalid or destructive query statement.