I’ve used sqlite3 before in an iPhone application, but how can I query out to an external SQL database sitting on my website?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Your app does not perform the actual query. The proper way to do this is to set up an API for your website so that the site does the querying and returns the appropriate data to your app.
Your API is generally going to be comprised of a number of end points that return data formatted for your app. For example, you might create a PHP or Python page that queries your database for all of the posts for a given user and returns them in a JSON or XML encoded array. (The data format is totally up to you. There are frameworks for parsing both formats available for iOS.)
For example, let’s say you have a PHP page on your server that returns the current day of the week. Your PHP would look something like this:
Let’s imagine that you’ve saved this as http://example.com/dayoftheweek.php
I recommend using the ASIHTTPRequest framework for performing HTTP requests from within your app. That said, let’s go ahead and set up a connection to your PHP page. Assuming you have ASIHTTPRequest all set up in your project, here’s what a request might look like:
Now, you need implement the ASIHTTPRequest Delegate methods, to parse the returned data. The method which handles a completed request is shown here:
To implement an API, your PHP pages would be more complex. You would build a more complex request , passing in variables and such, and the PHP page would act like a regular web service, returning the data that you have requested.