iOS:在iOS中处理数据库的最佳方法是什么?

时间:2022-09-11 14:58:37

Hello I was wondering what is the best way to deal with an external database on a server, like a local database on apache. Or similar when you use MYSQL. I guess I am confused how to do database work on iOS.

您好我想知道在服务器上处理外部数据库的最佳方法是什么,比如apache上的本地数据库。或者在使用MYSQL时类似。我想我很困惑如何在iOS上进行数据库工作。

Thank you, it is much appreciated.

谢谢,非常感谢。

4 个解决方案

#1


1  

You can't direct access MYSQL in iOS, although you are able to do it. Apple won't approve your app.

尽管您可以这样做,但您无法在iOS中直接访问MYSQL。 Apple不会批准您的应用。

So we are using Web Service to access the data. That mean you request your web server by specfic URL and parameters. And let your web server to query your db, and finally output them in plaintext, XML or JSON formats. When you got the result, you can parse it into an object like NSDictionary or NSArray.

所以我们使用Web Service来访问数据。这意味着您通过specfic URL和参数请求您的Web服务器。让您的Web服务器查询您的数据库,最后以纯文本,XML或JSON格式输出它们。获得结果后,可以将其解析为NSDictionary或NSArray等对象。

I believe the most common way of doing this is using ASIHTTPRequest and SBJson framework. It is fast and reliable. (Although ASIHTTPRequest had been stopped)

我相信最常见的方法是使用ASIHTTPRequest和SBJson框架。它快速可靠。 (虽然已停止ASIHTTPRequest)

Let say you are using PHP on an apache server.

假设您在Apache服务器上使用PHP。

For php - api.php:

$mysqlArray = mysql_query($query);
$resultObjs = array();
while($mysqlObj = mysql_fetch_object($mysqlArray)){
    array_push($resultObjs,$mysqlObj);
}

echo json_encode($mysqlObj);

For get the data with ASIHTTPRequest

NSURL *api = [NSURL URLWithString:@"http://YOURSERVER.com/api.php"];
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:api];
NSString *resultString = [request responseString];
[request release];

To parse it into an object using SBJson

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSArray *resultObjs = (NSArray*)[parser objectWithString:resultString];
[parser release];

This is a basic concept, read more example on their homepage and google some more examples.Hope it helps.

这是一个基本概念,在他们的主页上阅读更多示例,并谷歌更多的例子。希望它有所帮助。

#2


2  

Parse.com provides some interesting options in this area - you can upload a csv and create a relational DB with it, then access it through their SDK in your app, all in just a couple of lines of code.

Parse.com在这方面提供了一些有趣的选项 - 你可以上传一个csv并用它创建一个关系数据库,然后通过你的应用程序中的SDK访问它,所有这些只需几行代码。

I don't work for them but it's definitely worth checking out as a solution.

我不为他们工作,但绝对值得一试作为解决方案。

#3


2  

As Tom Jowett pointed before, if you are not sure what you really need, start with Parse.

正如Tom Jowett之前指出的那样,如果你不确定你真正需要什么,那就从Parse开始吧。

If you need to persist data locally (meaning on the phone), then Core Data is excellent. Core Data is not strictly an ORM, but a graph manager, but it will store data locally for you (and you don't have to worry about the actual db). You can also use SQLite directly, but you shouldn't (unless you have a really good reason to do that).

如果您需要在本地保存数据(意味着在手机上),那么Core Data非常出色。核心数据不是严格意义上的ORM,而是图形管理器,但它会为您本地存储数据(您不必担心实际的数据库)。你也可以直接使用SQLite,但你不应该这样做(除非你有充分的理由这样做)。

On the other hand, if you are going to just consume data from a server, then give parse.com a look, and if doesn't fits your needs, you can always write your own RESTful server, and use the db of your choice.

另一方面,如果您只是要从服务器中使用数据,那么请给parse.com一个外观,如果不符合您的需求,您可以随时编写自己的RESTful服务器,并使用您选择的数据库。

#4


1  

Simply use PHP pages on your server and reference them with GET variables through NSURLConnection. You could use an API, but this solution is much simpler.

只需在服务器上使用PHP页面,并通过NSURLConnection使用GET变量引用它们。您可以使用API​​,但此解决方案更简单。

If you need to get information from the server you could simply do the following:

如果您需要从服务器获取信息,您可以简单地执行以下操作:

NSString* returnedInfo = [NSString stringWithContentsOfURL:[NSURL urlWithString:@"http://www.myserver.com/myaccessorpage.php?a=sdf&s=dfg"]];

You can run it on a background thread to avoid app hanging as well.

您可以在后台线程上运行它以避免应用程序挂起。

This may not be the most efficient way to achieve what you are doing, but it is definitely the easiest way.

这可能不是实现您正在做的事情的最有效方式,但它绝对是最简单的方法。

Hope this helps you get started.

希望这有助于您入门。

#1


1  

You can't direct access MYSQL in iOS, although you are able to do it. Apple won't approve your app.

尽管您可以这样做,但您无法在iOS中直接访问MYSQL。 Apple不会批准您的应用。

So we are using Web Service to access the data. That mean you request your web server by specfic URL and parameters. And let your web server to query your db, and finally output them in plaintext, XML or JSON formats. When you got the result, you can parse it into an object like NSDictionary or NSArray.

所以我们使用Web Service来访问数据。这意味着您通过specfic URL和参数请求您的Web服务器。让您的Web服务器查询您的数据库,最后以纯文本,XML或JSON格式输出它们。获得结果后,可以将其解析为NSDictionary或NSArray等对象。

I believe the most common way of doing this is using ASIHTTPRequest and SBJson framework. It is fast and reliable. (Although ASIHTTPRequest had been stopped)

我相信最常见的方法是使用ASIHTTPRequest和SBJson框架。它快速可靠。 (虽然已停止ASIHTTPRequest)

Let say you are using PHP on an apache server.

假设您在Apache服务器上使用PHP。

For php - api.php:

$mysqlArray = mysql_query($query);
$resultObjs = array();
while($mysqlObj = mysql_fetch_object($mysqlArray)){
    array_push($resultObjs,$mysqlObj);
}

echo json_encode($mysqlObj);

For get the data with ASIHTTPRequest

NSURL *api = [NSURL URLWithString:@"http://YOURSERVER.com/api.php"];
ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:api];
NSString *resultString = [request responseString];
[request release];

To parse it into an object using SBJson

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSArray *resultObjs = (NSArray*)[parser objectWithString:resultString];
[parser release];

This is a basic concept, read more example on their homepage and google some more examples.Hope it helps.

这是一个基本概念,在他们的主页上阅读更多示例,并谷歌更多的例子。希望它有所帮助。

#2


2  

Parse.com provides some interesting options in this area - you can upload a csv and create a relational DB with it, then access it through their SDK in your app, all in just a couple of lines of code.

Parse.com在这方面提供了一些有趣的选项 - 你可以上传一个csv并用它创建一个关系数据库,然后通过你的应用程序中的SDK访问它,所有这些只需几行代码。

I don't work for them but it's definitely worth checking out as a solution.

我不为他们工作,但绝对值得一试作为解决方案。

#3


2  

As Tom Jowett pointed before, if you are not sure what you really need, start with Parse.

正如Tom Jowett之前指出的那样,如果你不确定你真正需要什么,那就从Parse开始吧。

If you need to persist data locally (meaning on the phone), then Core Data is excellent. Core Data is not strictly an ORM, but a graph manager, but it will store data locally for you (and you don't have to worry about the actual db). You can also use SQLite directly, but you shouldn't (unless you have a really good reason to do that).

如果您需要在本地保存数据(意味着在手机上),那么Core Data非常出色。核心数据不是严格意义上的ORM,而是图形管理器,但它会为您本地存储数据(您不必担心实际的数据库)。你也可以直接使用SQLite,但你不应该这样做(除非你有充分的理由这样做)。

On the other hand, if you are going to just consume data from a server, then give parse.com a look, and if doesn't fits your needs, you can always write your own RESTful server, and use the db of your choice.

另一方面,如果您只是要从服务器中使用数据,那么请给parse.com一个外观,如果不符合您的需求,您可以随时编写自己的RESTful服务器,并使用您选择的数据库。

#4


1  

Simply use PHP pages on your server and reference them with GET variables through NSURLConnection. You could use an API, but this solution is much simpler.

只需在服务器上使用PHP页面,并通过NSURLConnection使用GET变量引用它们。您可以使用API​​,但此解决方案更简单。

If you need to get information from the server you could simply do the following:

如果您需要从服务器获取信息,您可以简单地执行以下操作:

NSString* returnedInfo = [NSString stringWithContentsOfURL:[NSURL urlWithString:@"http://www.myserver.com/myaccessorpage.php?a=sdf&s=dfg"]];

You can run it on a background thread to avoid app hanging as well.

您可以在后台线程上运行它以避免应用程序挂起。

This may not be the most efficient way to achieve what you are doing, but it is definitely the easiest way.

这可能不是实现您正在做的事情的最有效方式,但它绝对是最简单的方法。

Hope this helps you get started.

希望这有助于您入门。