mySQL数据库到iPhone应用程序 [英] mySQL database to iPhone app

查看:296
本文介绍了mySQL数据库到iPhone应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络服务器上有一个MySQL数据库,需要从iPhone应用程序读取数据。什么是最好的方法做到这一点?数据需要从Web服务器访问,并将不时更新。是NSURL类别吗?

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?

感谢您的帮助。

推荐答案

我建议在服务器端使用一个简单的web技术 - PHP,Ruby on Rails等,作为一个基本的RESTful-ish Web服务,发送JSON。这是简单的,不需要很多的编程经验 - 通过PHP输出JSON需要一行代码,抓取数据的请求只需要少量的。

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.

在iPhone端, ASIHTTPRequest 可让您非常轻松地创建异步请求以进行更新和 SBJSON 将接收到的数据转换为NSDictionaries的NSArray非常简单,只需几行的代码。

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.

使它工作的关键是确保你密切跟踪数据修改日期,既确保新记录下载,并确保iPhone接收每个更新的记录。

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.

例如,MYSQL服务器上的每条记录可以有一个LastModified字段,一个使用 on update CURRENT_TIMESTAMP 命令保持字段至今。在iPhone上,你也将存储这些日期。当设备请求和更新它首先查找它正在更新的表的最近LastModified日期,并将其发送到服务器。服务器代码发回LastModified大于发送日期的所有记录。

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.

这可以通过在服务器端为每个记录设置一个DateCreated字段来进一步简化好。这允许服务器返回两个数据集,其中DateCreated字段大于发送的LastModified日期 - 这些将被插入到iPhone数据库中 - 一个集合,其中LastModified服务器日期晚于发送的日期但是DateCreated字段小于或等于发送的日期 - 这些是需要更新的数据行。

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.

这篇关于mySQL数据库到iPhone应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆