Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7934055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:31:29+00:00 2026-06-03T21:31:29+00:00

I have the follow SQL statement: const char* sqlStatement = SELECT ((? – deal.latitude)

  • 0

I have the follow SQL statement:

const char* sqlStatement = "SELECT ((? - deal.latitude) * (? - deal.latitude) + (? - deal.longitude) * (? - deal.longitude)) AS distance, id, title, shop, latitude, longitude FROM deal WHERE (type = ?) AND category IN (?) AND tribe IN (?) ORDER BY distance LIMIT 20;";

// ...

sqlite3_bind_double(preparedStatement, 1, location.latitude);
sqlite3_bind_double(preparedStatement, 2, location.latitude);
sqlite3_bind_double(preparedStatement, 3, location.longitude);
sqlite3_bind_double(preparedStatement, 4, location.longitude);
sqlite3_bind_int(preparedStatement, 5, type);
sqlite3_bind_text(preparedStatement, 6, [categories UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(preparedStatement, 7, [tribes UTF8String], -1, SQLITE_TRANSIENT);

Here, sixth and seventh arguments cause the query to fail, I mean the block inside my

while (sqlite3_step(preparedStatement) == SQLITE_ROW) {

isn’t executed. categories and tribes are build as follow:

NSArray* userCategories = [CategoryDataController getUserCategories];
NSMutableString* categories = [[NSMutableString alloc] init];

for (NSNumber *category in userCategories) {
    [categories appendString:[[NSString alloc] initWithFormat:@"%@, ", category]];
}
if ([categories length] > 0) {
    categories = (NSMutableString *)[categories substringToIndex:[categories length] - 2];
}

NSArray* userTribes = [TribeDataController getUserTribes];
NSMutableString* tribes = [[NSMutableString alloc] init];

for (NSNumber* tribe in userTribes) {
    [tribes appendString:[[NSString alloc] initWithFormat:@"%@, ", tribe]];
}
if ([tribes length] > 0)
    tribes = (NSMutableString *)[tribes substringToIndex:[tribes length] - 2];

userCategories and userTribes are arrays of NSNumber, and if I log tribes and categories I get well formed strings, that is something like:

1, 2, 3, 4, 5

The strange thing is that instead use the sqlite3_bind_ functions I build the query as follow:

NSString *sqlStatementNSString = [[NSString alloc] initWithFormat:@"SELECT ((%f - deal.latitude) * (%f - deal.latitude) + (%f - deal.longitude) * (%f - deal.longitude)) AS distance, id, title, shop, latitude, longitude FROM deal WHERE type = %d AND category IN (%@) AND tribe IN (%@) ORDER BY distance LIMIT 20;", location.latitude, location.latitude, location.longitude, location.longitude, type, categories, tribes];
const char *sqlStatement = [sqlStatementNSString UTF8String];

it works! What I’m doing wrong? Thanks beforehand (and please, sorry for my English).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:31:30+00:00Added an answer on June 3, 2026 at 9:31 pm

    I’ve searched for an answer to this, as well, and it seems that, in general, prepared statements don’t support the IN operator. There are all sorts of solutions which you can find on this site. My preferred solution (which was obtained from one of the answers) is to prepare a statement with a fixed number of slots in the IN operator list:

    SELECT * FROM db WHERE id IN (?, ?, ?, ?, ?, ?)
    

    Then you perform the query ((n + 5) / 6) times (in this example) and merge all the answers. If you have less than the number of slots, fill the rest with NULL, or duplicate the last entry. The optimizer should, hopefully, avoid the duplicate comparisons.

    Reusing a prepared statement is probably faster than building and running a custom query string (but profile to be sure!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the follow SQL query statement: SELECT subject, sender_list, date, uid FROM messages
I have an sql statement as follow: SELECT s.sp_id,s.synopsis,s.logline,s.user_id,s.bin_status,s.sp_order,s.sp_down_count,s.sp_name, `users`.firstname as `firstname`,`users`.lastname as `lastname`,s.status,
I have the follow sql statement in a program I am building, where id_list
I have a T-Sql Statement as follows; Insert into Table1 Select * From Table2
I have a table with sales per store as follows: SQL> select * from
I have been playing around with a quite complex SQL Statement for a few
i have an SQL statement wherein i am trying to update the table on
I have a prepared statement as so: private static final String SQL_LIST_GROUP = SELECT
I have this SQL statement below INSERT into 2011_electricity ( date, energy, daynum) VALUES
I have a sql statement which I would want to ORDER BY a specific

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.