StanLe
StanLe

Reputation: 5137

How to query an external SQL database - iPhone app

I've used sqlite3 before in an iPhone application, but how can I query out to an external SQL database sitting on my website?

Upvotes: 6

Views: 12415

Answers (4)

Moshe
Moshe

Reputation: 58067

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:

<?php
  echo date("l");
?>

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:

NSURL *url = [NSURL URLWithString:@"http://example.com/dayoftheweek.php"]
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];
[request setDelegate:self];

Now, you need implement the ASIHTTPRequest Delegate methods, to parse the returned data. The method which handles a completed request is shown here:

- (void)requestFinished:(ASIHTTPRequest *)request{
   //Here you would store the returned data and/or parse it
}

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.

Upvotes: 9

Xorsat
Xorsat

Reputation: 2408

@StanLe: You cannot use SQL Server or mySQL within native app directly. However you can communicate your application with external SQL Databases with following way.

  1. Create XML API using PHP, or C# or any tool which provide XML file in result.
  2. Use some parser (NSXMLParser) in xcode, which can read the tags of XML page.

Here is example

Here is sample Code

Another Very Simple Example

if still any confusion please write back.

Upvotes: 0

Rahul Sharma
Rahul Sharma

Reputation: 3013

The approach that is used to communicate with any external database is via Webservices (Which we are calling here as API). you make a request to server which is normally a function call on server and the server replies you with requested data.

You can go through This.As Far as PHP and other relevant terms are concerned then You will have to make some efforts and google some code. This can also be of some use . Normally the webservices written for iPhone make use of XML/JSON for iPhone. JSON/XML parser are available

Upvotes: 0

ySgPjx
ySgPjx

Reputation: 10245

I think you mean MySQL. Usually access to MySQL databases is forbidden from the outside, and for good reason. You may want to write an API that accesses your database instead. Your iPhone app would communicate with this API to insert and retrieve data.

Upvotes: 0

Related Questions