简体   繁体   中英

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. Or similar when you use MYSQL. I guess I am confused how to do database work on 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.

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.

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).

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.

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.

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. Apple won't approve your app.

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 .

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)

Let say you are using PHP on an apache server.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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