Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-12-2010, 03:46 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 26
borg359 is on a distinguished road
Default SQL Query Question

Hi everyone,
I have a pretty basic question about SQL queries. I have a database that contains the latitude and longitude as well as the name and other information of particular markers around the country. Finding all the markers within a certain area around the user's current location requires a complex query involving both the latitude and longitude. For example:

Code:
sprintf (nameCommand, "select name from Markers where(longitude between %f and %f AND latitude between %f and %f)", longitudeMin, longitudeMax, latitudeMin, latitudeMax);
sqlite3_exec(database, nameCommand, NameCallback, names, NULL);
Besides the name, there are 6 other parameters that I'd like to obtain for each of these markers and issuing this query 6 more times ends up taking an unacceptable amount of time.

So my question is this: Once I've identified which markers fulfill my search criteria, is there a way to use the index of that search to extract the other 6 parameters without having to search over the latitude and longitude columns 6 additional times? This would be something akin to:

Code:
i = where(LatitudeArray > x)
NameSubset = NameArray[i]
Property1Subset = Property1Array[i]
Property2Subset = Property2Array[i]
etc...
Any suggestions on how to get this done in SQL would be greatly appreciated!

-dan
borg359 is offline   Reply With Quote
Old 01-12-2010, 04:10 PM   #2 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 89
kalx is on a distinguished road
Default

Are the other parameters you want to obtain in the same table? If so you can easily fetch multiple parameters in one query by:

Code:
select name, AnotherColumnName, AnotherColumnName, etc from Markers where(longitude between %f and %f AND latitude between %f and %f)", longitudeMin, longitudeMax, latitudeMin, latitudeMax)
If they are in different tables you can most likely make use of the INNER JOIN operation to combine different tables and get all the data you want in one query.

- kalx
kalx is offline   Reply With Quote
Old 01-12-2010, 04:17 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 271
rarindeed is on a distinguished road
Default

Also, are you setting up indices? It would speed up query exec times.
__________________
@rarindeed


Burstly is the only open and free ad management platform for iPhone app developers.
rarindeed is offline   Reply With Quote
Old 01-12-2010, 04:52 PM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 26
borg359 is on a distinguished road
Default Great advice, need examples...

Thanks for the suggestion kalx. All of the parameters are indeed in a single table. The only problem is that my current callback functions are designed to expect only a single parameter list per query. Do you know of any examples of callback functions that can parse multiple parameters in a single query?

I've also read a lot about the performance improvements that can be achieved through indexing, but I haven't been able to find any workable examples of how this is done. If anyone knows of any, I'd love to see them. Thanks!


-dan
borg359 is offline   Reply With Quote
Old 01-12-2010, 06:26 PM   #5 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 89
kalx is on a distinguished road
Default

Quote:
Originally Posted by borg359 View Post
Thanks for the suggestion kalx. All of the parameters are indeed in a single table. The only problem is that my current callback functions are designed to expect only a single parameter list per query. Do you know of any examples of callback functions that can parse multiple parameters in a single query?

I've also read a lot about the performance improvements that can be achieved through indexing, but I haven't been able to find any workable examples of how this is done. If anyone knows of any, I'd love to see them. Thanks!


-dan
Hi Dan,
Are you sure about that? I've never used sql_exec and the callback feature, but from looking at the function, it looks it already supports multiple parameters.

Here's the prototype for the callback function:
Code:
int Callback(void *pArg, int argc, char **argv, char **columnNames)
int argc: Specifies the number of parameters/columns
char **argv: This is basically an array of char strings containing the value for each parameter/column
char **columnNames: This is an array of char strings containing the name of each parameter/column

Example:
Code:
int Callback(void *pArg, int argc, char **argv, char **columnNames) {

   //Loop through all the parameters
   for (int i=0; i < argc; i++) {
      NSLog(@"Column #%d Name:%s  with Value: %s", i, columnNames[i], argv[i]);
   }

}
I haven't tested this code so there could be some syntax errors, etc, but you should be able to get the gist of it.

The callback will get called for each record in the result set from your query.

- kalx
kalx is offline   Reply With Quote
Reply

Bookmarks

Tags
callback, index, query, sql

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 329
11 members and 318 guests
2Apps1Day, akacaj, Domele, GraffitiCircus, michelle, NetGuru, NSString, Paul Slocum, Sloshmonster, soohyun, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,114
Posts: 402,886
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:56 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0