开发者

How do you use "?" with sqlite statements in an executeQuery method?

I assume that you can include "?" in your sqlite statement, provided in the "executeQuery" method, you pass additional arguments corresponding to each "?". But when I put this to practice, the results are not consistent.

This statement works:

SELECT * FROM answers WHERE test_id = ? and question_id = ?

with this method

FMResultSet * answer = [[UIApp database] executeQuery:[Queries getTheAnswer], l, n];

The following statement crashes the program when I include a "?"

SELECT * FROM questions where test_id = ? ORDER BY RANDOM() LIMIT ?
FMResultSet * set = [[UIApp database] executeQuery:[Queries randomNumberOfVerses], selectedTests, numRounds];

But works fine when I hardcode a value in.

SELECT * FROM questions where test_id = 5 ORDER BY RANDOM() LIMIT ?
FMResultSet * set = [[UIApp database] executeQuery:[Queries randomNumberOfVerses], numRounds];

Any su开发者_运维技巧ggestions since I don't want to hardcode values in? All variable used as arguments are of type NSNumber*


I don't know the iPhone or Objective C, but usually ? arguments are used for prepared statements, rather than "immediate mode" queries.

In the SQLite C API, the functions you're interested in are called sqlite3_prepare*().

If you can figure out what the Objective C mapping is for those, you should be able make some progress.

Edit:

I almost forgot the part that actually answers your question...

Once you have a prepared statement, use the sqlite3_bind*() functions to give values to the query parameters.


While WHERE clauses can include bind-parameters, the LIMIT argument cannot. It has to be an integer literal. See the SQLite Syntax Diagram.

Given that, I'm not sure why your last one is working. It should be fine if you hardcode the LIMIT argument, leaving the test_id = ?, but it shouldn't work when you use ? for the LIMIT.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜