iOS Xcode 连接到 MySQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13401937/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:12:55  来源:igfitidea点击:

iOS Xcode connecting to MySQL database

ioscocoa-touchxcode4.5

提问by Hunter Harris

I'm currently making an iOS App for my school, and have no previous experience with iOS programming. However, I do have experience with HTML/PHP/MySQL.

我目前正在为我的学校制作 iOS 应用程序,并且之前没有 iOS 编程经验。但是,我确实有使用 HTML/PHP/MySQL 的经验。

Currently we have a basic tab bar application with 5 tabs at the bottom. Obviously, since this is an app for a school, it needs to be dynamic and hopefully we can set something up to connect to a database and retrieve school lunch menus, and news.

目前我们有一个基本的标签栏应用程序,底部有 5 个标签。显然,由于这是一个学校应用程序,它需要是动态的,希望我们可以设置一些东西来连接到数据库并检索学校午餐菜单和新闻。

I know how to connect to a database normally with PHP, but I have no idea how to do it using the current version of Xcode. I've done research and apparently I need to use a webservice and json to safely get the information from the database.

我知道如何使用 PHP 正常连接到数据库,但我不知道如何使用当前版本的 Xcode 进行连接。我已经做了研究,显然我需要使用 webservice 和 json 来安全地从数据库中获取信息。

Does anyone have any links for me to get started, because I have no experience at all with this program. I don't need to be spoonfed, but a point in the right direction would help. I really can't find any links for this new version of Xcode.

有没有人有任何链接让我开始使用,因为我对这个程序完全没有经验。我不需要用勺子喂食,但正确方向的一点会有所帮助。我真的找不到这个新版本 Xcode 的任何链接。

回答by J Shapiro

There are no "over the air" APIs that will let you connect directly to a MySQL database. Instead, what you could do is create a RESTful web service in front of the database that returns the data as JSON via a request from an NSURLRequest object.

没有可以让您直接连接到 MySQL 数据库的“无线”API。相反,您可以做的是在数据库前创建一个 RESTful Web 服务,该服务通过来自 NSURLRequest 对象的请求以 JSON 形式返回数据。

回答by Daniel

By normally connection via PHP, what you actually mean is you had done web development. This means there was a server, and a database.

通常通过 PHP 连接,您的实际意思是您已经完成了 Web 开发。这意味着有一个服务器和一个数据库。

There's no available server in the iOS runtime that will allow you to install PHP or MySQL.

iOS 运行时中没有允许您安装 PHP 或 MySQL 的可用服务器。

You can't do it...

你不能这样做...

But, you could make your php and database on a server, and connect to it via the network from your app.

但是,您可以在服务器上创建 php 和数据库,然后从您的应用程序通过网络连接到它。

My suggestion is to look what kind of database you need, if it's for school and it isn't really interactive with the outside world, then use SQLite in your app, it's an embedded database so it's on the phone, as quick as you can want, no networking and you can obviously leverage your SQL knowledge.

我的建议是看看你需要什么样的数据库,如果它是学校用的,它不是真正与外界交互的,那么在你的应用程序中使用 SQLite,它是一个嵌入式数据库,所以它在手机上,尽可能快想要,没有网络,您显然可以利用您的 SQL 知识。

If you want to make an API or, server with your php things, you really are giving yourself a lot of extra work.

如果你想用你的 php 东西制作 API 或服务器,你真的给自己做了很多额外的工作。

回答by János

You need to create an NSMutableURLRequestand initialize an NSURLConnectionwith it to send an HTTP message. Something like that:

您需要创建一个NSMutableURLRequest并用它初始化一个NSURLConnection以发送 HTTP 消息。类似的东西:

NSURL *url = [NSURL URLWithString:@"http://xyz.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];