[Mysql-cocoa-users] Re: MySQL Result in NSTable
Brought to you by:
sergecohen
From: Lorenz T. <lo...@te...> - 2003-03-11 18:37:58
|
Hi Rob P. I am working on an application called CocoaMySQL which uses the SMySQL framework, so I think I can give you some help. First of all I think the source of CocoaMySQL would give you some hints about using the framework. You can download it at cocoamysql.sourceforge.net. Especially the class CustomQuery contains code which could help you. In general, you have to do the following steps: 1. Set up a interface containing the NSTableView and set the class you are writing as the data source of the tableView. 2. Link the SMySQL framework to your project. 3. Implement methods similar to the following ones in your class (code not tested, so maybe there will be some errors while compiling): // ************************************************************************ ******************************************** in the interface: // ************************************************************************ ******************************************** #import <Cocoa/Cocoa.h> #import <SMySQL/SMySQL.h> @interface yourClass : NSObject { IBOutlet id tableView; SMySQLConnection *theConnection; NSArray *theResult; } - (void)connectToHost:(NSString *)host withLogin:(NSString *)name password:(NSString *)password usingPort:(NSString *)port; -(void)doQuery:(NSString *)queryString; // ************************************************************************ ******************************************** in the implementation: // ************************************************************************ ******************************************** - (void)connectToHost:(NSString *)host withLogin:(NSString *)name password:(NSString *)password usingPort:(NSString *)port /* makes the connection to the mysql server */ { theConnection = [[SMySQLConnection alloc] initToHost:host withLogin:login password:password usingPort:port]; } - (void)doQuery:(NSString *)queryString; /* performs the mysql-query and sets up the tableView columns */ { SMySQLResult *queryResult; int i; NSArray *theColumns; NSTableColumn *theCol; //perform query queryResult = [theConnection queryString:queryString]; if ( ![queryResult numOfRows] || !queryResult ) { //no rows in result //free tableView theColumns = [tableView tableColumns]; while ([theColumns count]) { [tableView removeTableColumn:[theColumns objectAtIndex:0]]; } theCol = [[NSTableColumn alloc] initWithIdentifier:@""]; [[theCol headerCell] setStringValue:@""]; [tableView addTableColumn:theCol]; [tableView sizeLastColumnToFit]; [tableView reloadData]; return; } //load full result in array (each row as a dictionary) theResult = [[queryResult fetch2DResultAsType:MCPTypeDictionary] retain]; //set columns //remove all columns theColumns = [tableView tableColumns]; while ([theColumns count]) { [tableView removeTableColumn:[theColumns objectAtIndex:0]]; } //add columns, corresponding to the query result theColumns = [queryResult fetchFieldsName]; for (i=0; i<[queryResult numOfFields]; i++) { theCol = [[NSTableColumn alloc] initWithIdentifier:[theColumns objectAtIndex:i]]; [theCol setEditable:NO]; [theCol setResizable:YES]; [theCol setDataCell:[[NSTextFieldCell alloc] initTextCell:@""]]; [[theCol headerCell] setStringValue:[theColumns objectAtIndex:i]]; [tableView addTableColumn:theCol]; } [tableView sizeLastColumnToFit]; [tableView reloadData]; } //tableView datasource methods - (int)numberOfRowsInTableView:(NSTableView *)aTableView /* here we return just the number of rows in the tableView */ { return [theResult count]; } - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex /* and here the value for the specified row and column */ { NSDictionary *theRow; NSString *theIdentifier = [aTableColumn identifier]; theRow = [theResult objectAtIndex:rowIndex]; if ( [[theRow objectForKey:theIdentifier] isKindOfClass:[NSData class]] ) return [[NSString alloc] initWithData:[theRow objectForKey:theIdentifier] encoding:[theConnection encoding]]; if ( [[theRow objectForKey:theIdentifier] isMemberOfClass:[NSNull class]] ) return @"NULL"; return [theRow objectForKey:theIdentifier]; } // ************************************************************************ ******************************************** Now you can call the method connectToHost:withLogin:password:usingPort: to make the connection and then the method doQuery: to perform a mysql query and load it in the tableView. I hope the code works. If you have questions, feel free to mail to me. Best Regards, Lorenz On Monday, March 10, 2003, at 09:24 PM, mys...@li... wrote: > Message: 1 > Date: Fri, 7 Mar 2003 18:09:17 -0500 > From: Rob P. <ro...@ez...> > To: mys...@li... > Subject: [Mysql-cocoa-users] MySQL Result in NSTable > > Someone please help... > > I'm new to Cocoa, and really need help learning on how to get a result > from a MySQL query, and putting the result into multiple fields on an > NSTableView. Any help would be greatly appreciated, thanks. > > Rob P. -- lorenz textor ** sh ** eigerstrasse 21 8200 schaffhausen tf 052 - 624 27 91 ** ticino ** via delle scuole 41 6963 pregassona tf 091 - 940 20 57 ** mobile 076 - 531 71 74 email lo...@te... |