Question

I have a MySQL database on a web server and need to read the data from an iPhone app. What is the best way to do this? The data needs to be accessed from the web server and will be updated from time to time. Is the NSURL Class the way to go?

thanks for any help.

Was it helpful?

Solution

I suggest a simple web technology on the server side -- PHP, Ruby on Rails, etc. -- that acts as a basic RESTful-ish web service that sends out JSON. This is straightforward and doesn't require a lot of programming expertise -- outputting JSON via PHP requires a single line of code, and the request to grab the data requires only a handful, for example.

On the iPhone side, ASIHTTPRequest makes it stunningly easy to create asynchronous requests for updates and SBJSON makes turning the received data into an NSArray of NSDictionaries incredibly easy, just a couple of lines of code.

The key to making it all work is making certain that you keep close track on data modification dates, both for ensuring new records are downloaded and for making certain the iPhone receives every updated record.

For example, each record on the MYSQL server could have a LastModified field, one that uses the on update CURRENT_TIMESTAMP command to keep the field up to date. On the iPhone you'll store these dates as well. When the device requests and update it first looks for the latest LastModified date for the table it's updating and sends that to the server. The server code sends back all records where LastModified is greater than the date sent in.

This can be further streamlined by having a DateCreated field on the server side for each record as well. This allows the server to return two sets of data, one where the DateCreated field is greater than the LastModified date sent in -- these will be inserts into the iPhone database -- and one set where the LastModified server date is later than the one sent in but the DateCreated field is less than or equal to the date sent in -- these are the data rows that need updating.

I just wrote an app that does this, so the issues are fresh in my mind. Hope this helps.

OTHER TIPS

You can not connect to the database directly. You will need to have a page on your server that dumps the necessary areas of your db in a JSON or XML format. You should look at TouchJSON.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top