Xcode连接到MS SQL数据库 [英] Xcode connect to the MS SQL database

查看:388
本文介绍了Xcode连接到MS SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在远程MS SQL server上建立并运行了一个现有数据库,并且我希望能够通过Xcode与该数据库进行通信和交互.我正在Swift中为OS X编写应用程序,该应用程序应使用的数据存储在该远程数据库中.

I have an existing database up and running on the remote MS SQL server, and I want to be able to communicate and interact with that database from Xcode. I am writing an application for OS X in Swift, and the data that the application should use, is stored in that remote database.

问题是,我似乎找不到任何可以连接到基于MS SQL server的数据库的Swift库.到目前为止,我只找到了这个开源库:

The problem is, I can't seem to find any Swift library that could connect to the MS SQL server based database. So far, I have only found this open-source library: SQLClient in Objective-C, but it's quite difficult to have it set, especially as I am not familiar with the Objective-C.

此外,在与数据库进行任何通信时,我总是会看到该Core-Data库被提及,但是据我了解,Core-Data不知道如何连接到MS SQL数据库.

Also, I keep seeing this Core-Data library being mentioned anytime there is some communication with the database, but as far as I understand Core-Data doesn't know how to connect to the MS SQL database.

在将Xcode Swift app连接到远程MS SQL数据库时,是否有人有经验?应该怎么做?任何建议都是不受欢迎的,因为现在我有点被这个问题困扰.

Does anyone have any experience in connecting the Xcode Swift app to the remote MS SQL database? How should one do this? Any kind of advice is more than welcome, because right now I am kind of stuck with this problem.

推荐答案

我建议使用php写一个Web服务,例如使用mysql数据并提供json/xml响应:这是一个代码示例,可以帮助您从数据库获取数据并将其解析为json.

I suggest to write a webservice with php for example to consume mysql data and provide a json/xml response : this is an example of code that can help you to getdata from Database and parse it to json .

function getOneAdminByUserName($cin){

require('connect.php');

//fetch table rows from mysql db
$sql = "SELECT * FROM `admin` WHERE cin= '".$cin."'";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection)); 


if(mysqli_num_rows($result)>0){
    $myArray = array();
while($row =mysqli_fetch_assoc($result))
{
   array_push( $myArray , $row);
}
return json_encode($myArray);   
//return json_encode(mysqli_fetch_assoc($result));
}
else{
return "no";
}

然后,您可以使用 AFnetworking

 NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

这篇关于Xcode连接到MS SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆