Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cabbit

macrumors 68020
Original poster
Jan 30, 2006
2,128
1
Scotland
I have a CLI application that can read and write to a mysql database. I would now like to start moving this to GUI based app and the first thing i want to do is show the results of a database table to a NSTableView.

So i started by creating a new Cocoa application without any options clicked and i moved over the class.

I then edited my AdminAppDelegate.m file with the following code.
Code:
#import "AB_Comforts_AdminAppDelegate.h"
#import "MysqlConnection.h"
#import "MysqlCommitException.h"
#import "MysqlRollbackException.h"
#import "GC_MYSQL_BIND.h"
#import "MysqlFetch.h"
#import "MysqlFetchField.h"

@implementation AB_Comforts_AdminAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	// Connect to the database
    MysqlConnection *connection = [MysqlConnection connectToHost:@"localhost"
															user:@"root"
														password:@""
														  schema:@"test_db"
														   flags:MYSQL_DEFAULT_CONNECTION_FLAGS];
	
	// Fetch the users from the database
	MysqlFetch *userFetch = [MysqlFetch fetchWithCommand:@"select username, email from users" onConnection:connection];
	
	// Print out a list of users
	NSLog(@"There are %d users",[userFetch.results count]);
	for (NSDictionary *userRow in userFetch.results) {
		NSNumber *userNumber = [userRow objectForKey: @"username"];
		NSString *userName = [userRow objectForKey: @"email"];
		NSLog(@"%@ %@",userNumber,userName);
	}
}

@end

And here is were i got stuck, my only .xib is MainMenu it has a menu and a window so i put in a NSTableView and tried the trusty ctrl click to match it up with the controller as i have done with some other applications(built with the core data and document based selected) however this did not work.

Any idea where about i am going wrong, my aim for now is just to get my data on the gui instead of the console log.
 
If you are just trying to display the data in a GUI field rather than a console, then NSTableView is the wrong class to use for this. You might want to use an NSTextField instead. You would then have to append the output to the NSTextField rather than passing it to NSLog.

If you really do want to use a table view for some reason, you need to create an object that conforms to the NSTableViewDataSource protocol. One way to do this is to create a new class following this protocol, with an additional method to receive the data and organize it. Then instantiate this class in applicationDidFinishLaunching: and call setDataSource: on the NSTableView, passing the object. Any time you add data to the data source object you will need to call reloadData on the NSTableView, though.
 
I should have explained better, the application is eventually going to be a admin for my Ecommerce and forum i have built with Ruby on Rails 3.0. I wish to make the admin a native Mac OS application and perhaps in the future a iPad application so i can manage my store better.

I am going for a Cocoa application in part to learn Cocoa and Objective C, and also in part to use the CPU for some heavy lifting tasks like compressing a video before upload.

Now with this in mind i think the best way to display database tables is to put them into a table so this is why i went for NSTableView.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.