I come from a PHP background as a web developer, so when I started learning Objective C, one of the first programs I tried to write was an RSS reader since I had plenty of data laying around in MySQL tables. I googled for MySQL frameworks for Cocoa/Objective C and only came up with one, MCPKit, formerly called SMySQL by Serge Cohen and available on his site at http://mysql-cocoa.sourceforge.net/. Unfortunately the framework available from there is old and compiled only for PPC, not Intel. Since then, someone called glimberg has put a Universal Binary version of the earlier SMySQL project online at http://code.google.com/p/mysql-cocoa-framework/, However I believe this framework is broken as I found it wouldn’t return more than one column of data. I contacted some of the developers of the project Sequel Pro as they were the only people I knew of who used MCPKit in their application and they were very helpful and told me how to compile their more updated and fixed version of MCPKit that they bundle with their application. I did, and finally the framework sprang to life. I’d like to share with you how to install the framework and how to write a simple GUI application that displays query results in a table. I have a database of RSS news items that I want to display in a scrollable table.
Install the framework into your project
The best guide to this is this one written by Serge himself. I’ve stored it here in case it goes missing from his site like his examples and other code. I recommend you read it and follow the instructions to the letter to avoid linking errors.
Include the framework
In your main App Controller’s header file, include the framework’s headers:
#import <MCPKit/MCPKit.h>
Setup your database variables
In your header, define the variables we’re going to use to store the connection handle, the results, and the array we use.
MCPConnection *db;
MCPResult *result;
Connect to your database
The syntax for connecting to the database is very easy. The authors of Sequel have changed the way passwords are handled in their version of MCPKit to support connecting via keychain, so here’s how it’s done:
db = [[MCPConnection alloc] initToHost:@"hostname" withLogin:@"username" usingPort:3306];
[db setPassword:@"yourpassword"];
NSLog(@"Connect: %d", [db connect]);
NSLog(@"SelectDB: %d", [db selectDB:@"ismyhome"]);
Running Queries
Queries are easy to perform and results can be retrieved as an array or an NSDictionary which is quite useful. If you’re used to PHP, you’ll use queryString instead of mysql_query and fetchRowsAsArray and fetchRowsAsDictionary instead of mysql_fetch_array and mysql_fetch_assoc like you’re used to. In my example here, I just use fetchRowAsArray.
NSArray *row;
result = [db queryString:@"select Title, Link from newsitems LIMIT 10"];
while (row = [result fetchRowAsArray]) {
[rss addObject: [row objectAtIndex:0]];
}

Creating an NSTableView
OK so you’ve got some data, how do you output it ? In your header, create a tableView outlet.
IBOutlet NSTableView *tableView;
Now in Interface Builder, drop in an NSTableView and an Object for your AppController. Click twice on the middle of the table so the TableView is selected (as opposed to the ScrollView), then right click and connect the dataSource and delegate methods to your AppController so it looks as shown in the screenshot to the right. To link our IBOutlet to the NSTableView, right click your AppController and link the tableView outlet to your NSTableView you just created.
Provide data to your NSTableView
To provide your NSTableView with data you must implement two protocol functions as shown below:
-(int)numberOfRowsInTableView:(NSTableView *)tv {
return [rss count];
}
-(id)tableView:(NSTableView *)tv
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row {
return [rss objectAtIndex:row];
}
Download the the working framework
As mentioned, I found both the versions of MCPKit/SMySQL floating around on Serge’s page and the google page to be broken, so I’m providing here a copy of the Universal Binary compiled framework as compiled from the latest Sequel Pro 0.9.7 source. If you need to recompile the framework for any reason, I suggest getting their source below. To compile it you just need to select the MCPKit target and build.
Sequel Pro Source Code – download
Latest MCPKit (build 1218) – download
Getting Help
Since Serge is no longer maintaining this project it’s hard to find support for it. If you’re looking for other users of the framework, try the authors of Sequel Pro who can be found in #sequel-pro on Freenode. Thanks to avenjamin for helping me with some linking errors I was having and putting me on the right track.