views:

38

answers:

2

I am reviewing code that makes the following call:

id<PLResultSet> results = [sqliteDatabase executeQuery:@"select * where id=?",Id];

sqliteDatabase is an instance of PlausibleDatabase (from GoogleCode, I gather). The code works, but I don't understand it. Specifically, how does this section work?

@"select * where id=?",Id

Is the query being made with ? being replaced by Id? Or is the exeuteQuery function somehow combining the strings? How does this syntax make sense.

(Yes, I am new to Obj-C)

Thanks,

KF

A: 

It's specific to the method executeQuery, in which ? is used as a placeholder, and the appropriate positional argument is used as filler for that placeholder (with quoting, etc. added as necessary).

mipadi
I would never have figured that out. Thanks!
Kevin Franklin
@mipadi: One benefit of parameterized queries is that no quoting has to be done at all: the parameter values can be passed to the database as distinct pieces of data.
Douglas
+1  A: 

This bit:

@"select * where id=?"

is an NSString (as opposed to a c-style string) which is being passed into a executeQuery: : method on the sqliteDatabase object. The second (unnamed) argument to the method is Id, presumably a local variable.

Guessing from the name of the method, the sqlite wrapper probably creates a parameterized query. The question mark is the syntax used by sqlite to mark where to insert the parameters.

Douglas