Query SQL Server Database from Native iOS Application

Query SQL Server Database from native iOS Application

A web service is indeed the only way, but Red Gate's written one you can reuse:

http://www.mobilefoo.com/iSqlServerSDK.html

http://labs.red-gate.com/Tools/Details/iSqlSDK

It's not officially released yet, just in beta, so keep in mind that features & prices may change.

How to connect to SQL Server Database from ios application?

Well you can move the database to Microsoft Azure SQL database and use the iOS mobile toolkit provided by Microsoft connect to it.

How to query an external SQL database - iPhone app

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.

How to connect an iOS app to SQL server without webservices?

For SQL server, you can use this library:
SQLClient by martinrybak

SQLClient: Native Microsoft SQL Server client for iOS. An Objective-C wrapper around the open-source FreeTDS library.

Connecting my iPad app to a SQL Server

I am currently working in an iOS application that requires this same functionality. For mySQL database queries on the server, I am using server-side PHP scripts that can accept variables, such as the table name or database search term.

What I do is I make an HTTP GET request using objective-C's NSMutableURLRequest, then have the server process the request (in PHP), and then return the database query results to my application in JSON format. I use SBJsonParser to parse the returned data into an NSData, and then an NSArray object.

An example of making an HTTP request in Objective-C:

NSString *urlString = [NSString stringWithFormat:@"http://website.com/yourPHPScript.php?yourVariable=something"];
NSURL *url = [NSURL URLWithString: urlString];

NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] initWithURL:url];

/* set the data and http method */
[request1 setHTTPMethod:@"GET"];
[request1 setHTTPBody:nil];

/* Make the connection to the server with the http request */
[[NSURLConnection alloc] initWithRequest:request1
delegate:self];

There is more code that you need to add to actually respond to the request when it returns, and I can post an example of that if you would like.

I actually dont know if this is the best way to do this, but It has worked for me so far. It does require that you know PHP though, and I don't you if you have any experience with it.


UPDATE:

Here is some sample code showing how to respond to the request. In my case, since I am getting a JSON encoded response, I use the SBJsonParser to parse the response.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
/* This string contains the response data.
* At this point you can do whatever you want with it
*/
NSString *responseString = [[NSString alloc] initWithData:receivedData
encoding:NSUTF8StringEncoding];

/* Here I parse the response JSON string into a native NSDictionary using an SBJsonParser */
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];

/* Parse the JSON into an NSDictionary */
NSDictionary *responseArr = [parser objectWithString:responseString];

/* Do whatever you want to do with the response */

/* Relsease the connection unless you want to re-use it */
[connection release];
}

Also add these methods, assuming you have an NSMUtableData instance variable titled receivedData.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}

show a list of data in react native app from sql server

Ok here are the steps to follow:

  1. Use flatlist
  2. Your data source should be a regular array. So, you can use the response from your api as it is.
  3. renderRow should change as renderItem
  4. renderSeparator should change as ItemSeparatorComponent

here is the final form:

const[isLoading,setIsLoading]=useState(true);
const [data, setData]=useState([]);

.......

fetch('http://localhost/api/findid.php',{
mode:'no-cors'
})
.then((response) => response.json())
.then((responseJson) => {
//you can't use setState inside function components. :)
setData(responseJson);
setLoading(false);
})
.catch((error) => {
console.error(error);
})

.......

<FlatList
data={data}
keyExtractor={item => item.jobid} //it should be unique. change it
renderItem={({item}) => <Text>{item.job}</Text>}
/>

This should solve your issue for now but keep in mind these too:

  1. Looks like you don't know differences between class components and function components. Do your research
  2. Flatlist has it's own performance configurations. Research and implement them.

Can I use Sql Server Database from iOS?

In your question, you linked to a management API for SQL Database service - this has nothing to do with data manipulation; it's strictly a management API for dealing with servers and databases.

If you want to direct-connect from an IOS app to SQL Database Service (or SQL Server in a VM), you'd need to find a client library for IOS (and recommendations of such a library are not in scope for StackOverflow). There is no native SQL Server client library specifically provided through the Azure SDKs / APIs.

Outside of a native client library, there is Azure Mobile Services, which provides an API stack specifically designed around use by mobile apps (whether IOS, Android, Windows Phone, or even JavaScript). The API stack provided here, by default, gives CRUD operations for SQL Database tables. Additionally, it supports adding custom API calls, where you have complete control of your API calls.

Any other API stack would require you to choose the stack and run it yourself (whether in a Linux or Windows VM, Cloud Service, or Web App - there's no single right way to do this).

Documentation for Azure Mobile Apps (and related API feature, such as monitoring) is here.



Related Topics



Leave a reply



Submit