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 7158659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:06:11+00:00 2026-05-28T13:06:11+00:00

I have sqlite3 database with locations which have a langitude and a longitude value.

  • 0

I have sqlite3 database with locations which have a langitude and a longitude value. I want to use a SELECT statement to get the locations that are within a specific radius.

I tried to implement that as described here: http://www.thismuchiknow.co.uk/?p=71 and its all working fine if my query looks like this:

"SELECT pk FROM location WHERE distance(latitude, longitude, '%d', '%d') > 100"

but if I use the “<” operator instead of “>”, the statement can’t be executed, I get “unknown error” for sqlite3_errmsg(database). I really can’t figure out why using another operator causes an error. Anyone got an idea?

my code:

    NSMutableArray *resultsArray = [[NSMutableArray alloc] init];
    self.results = resultsArray;
    [resultsArray release];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Locations.sqlite"];

    if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

        sqlite3_create_function(database, "distance", 4, SQLITE_UTF8, NULL, &distanceFunc, NULL, NULL);


        double vLongitude;
        double vLatitude;
        vLongitude = 8.683;
        vLatitude = 50.117;

        sql = [[NSString stringWithFormat:@"SELECT pk FROM location WHERE distance(latitude, longitude, '%d', '%d') < 50", vLatitude, vLongitude]cStringUsingEncoding:NSUTF8StringEncoding];
        sqlite3_stmt *statement;

        if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK){

                while(sqlite3_step(statement) == SQLITE_ROW) {
                    //the second parameter indicates the column index to the result set
                    int primary_key = sqlite3_column_int(statement, 0);
                    Location *loc = [[Location alloc] initWithPrimaryKey:primary_key database:database];
                    [results addObject:loc];
                    [loc release];
                }
            }
        printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );
        sqlite3_finalize(statement);
    }

    else {
        sqlite3_close(database);
        NSAssert1(0, @"Failed to open database with message '%s'.",  sqlite3_errmsg(database));
    }
    searchResultsController.results = self.results;

and the distance function from the link above:

static void distanceFunc(sqlite3_context *context, int argc, sqlite3_value **argv)
    {
        // check that we have four arguments (lat1, lon1, lat2, lon2)
        assert(argc == 4);
        // check that all four arguments are non-null
        if (sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL || sqlite3_value_type(argv[2]) == SQLITE_NULL || sqlite3_value_type(argv[3]) == SQLITE_NULL) {
            sqlite3_result_null(context);
            return;
        }
        // get the four argument values
        double lat1 = sqlite3_value_double(argv[0]);
        double lon1 = sqlite3_value_double(argv[1]);
        double lat2 = sqlite3_value_double(argv[2]);
        double lon2 = sqlite3_value_double(argv[3]);
        // convert lat1 and lat2 into radians now, to avoid doing it twice below
        double lat1rad = DEG2RAD(lat1);
        double lat2rad = DEG2RAD(lat2);
        // apply the spherical law of cosines to our latitudes and longitudes, and set the result appropriately
        // 6378.1 is the approximate radius of the earth in kilometres
        sqlite3_result_double(context, acos(sin(lat1rad) * sin(lat2rad) + cos(lat1rad) * cos(lat2rad) * cos(DEG2RAD(lon2) - DEG2RAD(lon1))) * 6378.1);
    }
  • 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-05-28T13:06:12+00:00Added an answer on May 28, 2026 at 1:06 pm

    “SELECT pk FROM location WHERE distance(latitude, longitude, ‘%d’, ‘%d’) < 50”
    latitude and longitude are double type you should do %f not %d

    About SQL error, try to do:

    SELECT pk, distance(latitude, longitude, '%f', '%f') AS distance_km FROM location WHERE disntace_km < 50
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Sinatra DataMapper app hitting a sqlite3 database that I am attempting
I have SQLite3 database, which is populated with some large set of data. I
I have a query for sqlite3 database which provides the sorted data. The data
I have data stored in a sqlite3 database and have several places that I
I have the following statement in Rails 3 using an SQLite3 database: word =
Hello I have a Sqlite3 database created in my computer and I want to
I have got an SQLite3 Database with several images in it. I want to
I have an iPhone application that uses a sqlite3 database for saving data and
I am working on a c# project which makes use of an SQLite3 database
I have a database that I want to add a column to, I changed

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.