简体   繁体   English

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

[英]iOS: In iOS what is the best way to deal with a database?

Hello I was wondering what is the best way to deal with an external database on a server, like a local database on apache. 您好,我想知道处理服务器上的外部数据库(例如apache上的本地数据库)的最佳方法是什么。 Or similar when you use MYSQL. 使用MYSQL时也是如此。 I guess I am confused how to do database work on iOS. 我想我很困惑如何在iOS上进行数据库工作。

Thank you, it is much appreciated. 谢谢,非常感谢。

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. 我不为他们工作,但是绝对值得一试作为解决方案。

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非常适合。 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). 严格地说,Core Data不是一个ORM,而是一个图形管理器,但是它将为您本地存储数据(并且您不必担心实际的数据库)。 You can also use SQLite directly, but you shouldn't (unless you have a really good reason to do that). 您也可以直接使用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服务器,并使用您选择的数据库。

Simply use PHP pages on your server and reference them with GET variables through NSURLConnection . 只需在服务器上使用PHP页面,然后通过NSURLConnection使用GET变量引用它们即可。 You could use an API, but this solution is much simpler. 您可以使用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. 希望这可以帮助您入门。

You can't direct access MYSQL in iOS, although you are able to do it. 尽管可以,但是无法在iOS中直接访问MYSQL。 Apple won't approve your app. 苹果不会批准您的应用。

So we are using Web Service to access the data. 因此,我们正在使用Web Service访问数据。 That mean you request your web server by specfic URL and parameters. 这意味着您需要通过特定的URL和参数来请求Web服务器。 And let your web server to query your db, and finally output them in plaintext , XML or JSON formats. 然后让您的Web服务器查询您的数据库,最后以plaintextXMLJSON格式输出它们。 When you got the result, you can parse it into an object like NSDictionary or NSArray . 得到结果后,可以将其parseNSDictionaryNSArray类的对象。

I believe the most common way of doing this is using ASIHTTPRequest and SBJson framework. 我相信最常用的方法是使用ASIHTTPRequestSBJson框架。 It is fast and reliable. 快速可靠。 (Although ASIHTTPRequest had been stopped) (尽管ASIHTTPRequest已停止)

Let say you are using PHP on an apache server. 假设您在apache服务器上使用PHP。

For php - api.php: 对于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 用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 使用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. 这是一个基本概念,请在其首页上阅读更多示例,并在Google上阅读更多示例。希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM