IPHONE: MySQL on iPhone

Before we go on, I'm going to assume you already know that the simplest way to achieve MySQL data connection is by writing a short scriptlet in PHP/JSP, etc. While simpler, it's security is somewhat problematic, since sniffers may be able to pick up the URL requests.

After hours of research I found what seems like the only solution: Cocoa-MySQL + MySQL C-API

According to this blog, all you seem to need is Cocoa-MySQL, but no matter what I tried, I just couldn't seem to get the binaries to link correctly. Not to mention, Apple has a pretty strict limitation on private frameworks, and this solution probably goes against one of their 20 NDAs.

This is where MySQL C-API comes in handy. I downloaded Mac OS X 10.5 (TAR package) for my x86 Intel system. Extract the zip and grab the /lib and /include folders and throw them into some local directory (NOT IN YOUR XCODEPROJ, see steps 4, 5 below).

From the Cocoa-MySQL site, download MCPKit_src_XXX.dmg, and grab the contents of /MCPFoundationKit and throw it into your XCODEPROJ. These are great wrapper classes that help facilitate the connection & query processes.

If you tried to build the app right now, you'd get errors stating missing headers and mysql_xxx_xxx functions. That's because we haven't set up the project settings yet.

1. In Xcode, goto Project > Edit Project Settings.
2. Select the Build tab.
3. Under Linking > Other Linking FLags, add the following flags: -lmysqlclient_r, -lmysqld, -lmysqlclient, -lm, -lz
4. Under Search Paths > Header Search Paths:
5. Under Search Paths > Library Search Paths:
6. Add #import "MCPKit_bundled.h" to the header of any class accessing MySQL
7. Edit MCPKit_bundled.h by changing all #import to #import "XXX.h"

Now you should be able to build, and use the sample code from the blog previously mentioned to directly connect to a remote MySQL server.

2 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

Hello, i did reference to this page.
and able to issue a query in simulator mode.

However, Device mode is "mysql_xxx referenced from" error occurs.
If you know please tell me how to solve...