How to query an external SQL database - iPhone app

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

+6
source share
4 answers

Your application does not fulfill the actual request. The right way to do this is to configure the API for your site so that the site fulfills the request and returns the appropriate data to your application.

Your API will typically consist of several endpoints that return data formatted for your application. For example, you can create a PHP or Python page that queries your database for all messages for a given user and returns them in a JSON or XML array. (The data format is completely up to you. There is a framework 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 will look something like this:

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

Imagine you saved this as http://example.com/dayoftheweek.php

I recommend using ASIHTTPRequest to make HTTP requests from your application. However, release and configure the connection to your PHP page. Assuming you have ASIHTTPRequest configured in your project, here is what the request looks like:

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

Now you need to implement ASIHTTPRequest delegation methods to analyze the returned data. Here is a method that processes a completed request:

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

To implement the API, your PHP pages will be more complex. You would build a more complex query by passing variables and such, and the PHP page would act like a regular web service, returning the data you requested.

+9
source

I think you mean MySQL. Usually access to MySQL databases is forbidden from the outside and for good reason. You might want to write an API that accesses your database. Your iPhone application will contact this API to insert and retrieve data.

0
source

@StanLe: you cannot directly use SQL Server or mySQL in a native application. However, you can link your application to external SQL databases as follows.

  • Create an XML API using PHP or C # or any tool that provides the result of an XML file.
  • Use some parser (NSXMLParser) in xcode that can read XML page tags.

Here is an example

Here is a sample code

Another very simple example.

If there is any confusion, please write.

0
source

The approach that is used to communicate with any external database is through Webservices (which we call the API here). you make a request to the server, which is usually a function call on the server, and the server responds with the requested data.

You can go through It . As for PHP and other relevant terms, you will have to make some effort and google some code. It may also be useful. Typically web services written for the iPhone use XML / JSON for the iPhone. JSON / XML parser available

0
source

Source: https://habr.com/ru/post/892060/


All Articles