xcode 将我的 iPad 应用程序连接到 SQL Server
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11462158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting my iPad app to a SQL Server
提问by
I have an iPad application that uses table view controllers to present lists of data. I want to connect these tables to data in a SQL Server. I have very little experience in this realm of programming and I am not sure where to begin. In the long run I'd like adding/editing/deleting data on the app to sync with the server as well. I understand that this is a very broad question, so I am mainly looking for suggestions to help me get started. I, for example, do not want to start researching and learning Core Data if it is not the framework that can accomplish my goal.
我有一个 iPad 应用程序,它使用表视图控制器来呈现数据列表。我想将这些表连接到 SQL Server 中的数据。我在这个编程领域的经验很少,我不知道从哪里开始。从长远来看,我希望在应用程序上添加/编辑/删除数据以与服务器同步。我知道这是一个非常广泛的问题,所以我主要是在寻找帮助我入门的建议。例如,如果不是可以实现我的目标的框架,我不想开始研究和学习 Core Data。
In short, how can I connect my application to a SQL Server so that I can access its data and sync it to a device? Any example code/walkthroughs would be much appreciated.
简而言之,如何将我的应用程序连接到 SQL Server,以便我可以访问其数据并将其同步到设备?任何示例代码/演练将不胜感激。
采纳答案by bddicken
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.
我目前正在使用需要相同功能的 iOS 应用程序。对于服务器上的 mySQL 数据库查询,我使用可以接受变量(例如表名或数据库搜索词)的服务器端 PHP 脚本。
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 SBJsonParserto parse the returned data into an NSData, and then an NSArray object.
我所做的是使用objective-C 的NSMutableURLRequest 发出HTTP GET 请求,然后让服务器处理请求(在PHP 中),然后将数据库查询结果以JSON 格式返回给我的应用程序。我使用SBJsonParser将返回的数据解析为 NSData,然后是 NSArray 对象。
An example of making an HTTP request in Objective-C:
在 Objective-C 中发出 HTTP 请求的示例:
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.
我实际上不知道这是否是最好的方法,但到目前为止它对我有用。不过,它确实要求您了解 PHP,如果您有任何相关经验,我不知道。
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.
下面是一些示例代码,展示了如何响应请求。就我而言,由于我收到的是 JSON 编码的响应,因此我使用 SBJsonParser 来解析响应。
- (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.
还要添加这些方法,假设您有一个名为 receivedData 的 NSMUtableData 实例变量。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
回答by Krumelur
I would not recommend connecting directly to a SQL server. You could, in theory, try to compile FreeTDSor unixODBC for iOS. I did not try it, but from my experience, at least FreeTDS should be fairly portable.
我不建议直接连接到 SQL 服务器。理论上,您可以尝试为 iOS编译FreeTDS或 unixODBC。我没有尝试过,但根据我的经验,至少 FreeTDS 应该是相当便携的。
However, there are many other (and for most purposes better and easier) way of synchronizing data with a SQL Server. For example, you could use RestKiton the iPad side and a simple REST service on the SQL Server side. Or you could use ODatawhich has a iOS library(which I just recently learned about, I have to admit).
但是,还有许多其他(并且对于大多数目的而言更好、更简单)与 SQL Server 同步数据的方法。例如,您可以在 iPad 端使用RestKit,在 SQL Server 端使用一个简单的 REST 服务。或者您可以使用具有iOS 库的OData(我最近才知道,我不得不承认)。