SQL Programming in Objective-C/Cocoa

Discussion in 'Mac Programming' started by ryanzec, Feb 23, 2010.

  1. ryanzec macrumors member

    Joined:
    Feb 23, 2010
    #1
    I am switching my main platform from Windows to Mac and the first thing I want to do is build a Project Management System using Objective-C/Cocoa. I also want the backend of the system to be in MySQL so that if I want to or there is a demand for it, I want to be able to build web interface to the Project Management System (and MySQL is my database or choice for web development). The only thing I can find is mysql-cocoa and that have 2 problems. First is that is has not been updated since 2006 and the second is that it is GPL (I need to license that allows me to use it in a commercial application). Is there currently any way to connect to a remote MySQL database with Objective-C/Cocoa?
     
  2. oneeach macrumors member

    Joined:
    Feb 12, 2010
    #2
    Objective-C is an extension to C so any C library can be used as-is. I don't use MySQL, but it looks like there is a C API that you can use inside your own Objective-C classes, no wrapper needed.
     
  3. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #3
    If you're willing to switch to PostgreSQL, you can take a look at PostgreSQL for Mac which is released under the BSD. Postgres is similar to MySQL in many respects, has a friendlier license, and will scale much better than MySQL. There's also a paid framework for interacting with a Postgres backend using Cocoa called BaseTen that is very Core Data-like in it's implementation.
     
  4. ryanzec thread starter macrumors member

    Joined:
    Feb 23, 2010
    #4
    While using PostgreSQL is not something I would 100% against, I would rather Use MySQL. I wonder why there are no Cocoa based MySQL implementations.
     
  5. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #5
    MySQL is released under the GPL, so you're not going to find BSD licensed toolkits that interact with it.
     
  6. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #6
    No reason why something that is a Cocoa toolkit has to be BSD licensed. There are plenty of Cocoa apps and libraries using the GPL today.
     
  7. ryanzec thread starter macrumors member

    Joined:
    Feb 23, 2010
    #7
    How do libraries like PHP get away with it. PHP links to MySQL and the PHP license is BSD-like (no copyleft restrictions like GPL has). Is PHP fine since they release there source code anyways? What about the number of web applications that use MySQL that are under commercial licenses?

    I though that license only pertained if you either include the source code in your application or statically link the library in your application. I didn't think if you just connect to a MySQL database that that meant you have to follow the GPL license.

    I am not saying your wrong because just thinking about GPL/LGPL vs MIT/BSD and what you can and can't do and all that stuff just gives me a headache. That is why I try to avoid using GPL licensed code when I work on my own projects. Of course I have been mainly doing web development with PHP and maybe things are a little bit different with that version do compiled desktop development.
     
  8. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #8
  9. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #9
    The OP specifically stated that he was looking for something that wasn't under the GPL.

    @ryanzec, I honestly don't understand all of the nuances of using GPL code, dynamic linking vs. static linking, etc (nor do I really want to learn them). It's just easier for me to avoid the GPL and stick to the less burdensome licenses. That's part of the reason why I wanted to learned Postgres instead of MySql: I can do whatever I want and not ever have to think/worry about those issues.

    Also, Objective-C has a runtime that is dynamically making changes to code on-the-fly which may be part of the reason why the GPL's viral clause gets invoked. I'm sure smarter people than me would be able to explain it.

    I don't want to get into a GPL holy war debate. Just trying offer some suggestions to the OP.
     
  10. ryanzec thread starter macrumors member

    Joined:
    Feb 23, 2010
    #10
    Yea, thanks for the advice. Thinking about it more, it would not hurt to know another database system beside MySQL so I think I just might venture into PostgreSQL for this project.

    @ GorillaPaws: Do you use PostgreSQL for Mac, BaseTen, or something else? PostgreSQL for Mac looks good but seems to have very little documentation.
     
  11. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #11
    For PostgreSQL for Mac, if you download the installer, you'll find a document in the document's directory called "Getting Started with PGSQLKit.pdf" it outlines a simple project. Here's the what the code looks like:

    Code:
    //PGVIController.h
    #import <Cocoa/Cocoa.h> 
    #import <PGSQLKit/PGSQLKit.h>
    
    @interface PGVIController : NSObject 
    {
        IBOutlet NSTextField *serverVersion;
    }
    @end
    
    Code:
    //PGVIController.m
    #import "PGVIController.h"
    
    @implementation
    -(void) awakeFromNib
    {
    	PGSQLConnection *connection = [[PGSQLConnection alloc] init];
    	
    	[connection setUserName: @"postgres"];
    	[connection setPassword: @""];
    	[connection setServer: @"localhost"];
    	[connection setPort: @"5432"];
    	[connection setDatabaseName: @"postgres"];
    	
    	if( [connection connect] )
    	{
    		NSString *cmd;
    		cmd = [NSString stringWithString: @"select verison() as version"];
    		PGSQLRecordset *rs = [connection open: cmd];
    		
    		if( ![rs is EOF] )
    		{
    			[serverVersion setStringValue: [[rs fieldByName: @"version"] asString]];
    		}
    		[rs close];
    		[connection close];
    	}
    	else
    	{
    		NSLog( @"Connection Error: %@", [connecion lastError] );
    	}
    }
    @end
    
    It's all pretty straightforward. PGSQLKit is just an Objective-C wrapper class for interfacing directly with the server using SQL.

    BaseTen(not free for commercial use) is very cool because it allows you to abstract away the SQL code and treat your Postgres database as if it were a Core Data Model. Here's a link to the tutorial for their framework.

    Also, here's a general gotcha I ran into when trying to set up the PostgreSQL database from the terminal. Initially you'll only have access to the database as user "postgres" which doesn't have a password by default, so there's no way to login as postgres. The way around this dilemma is to use the command:
    Code:
    sudo su postgres
    which will allow you to do your initial setup as user postgres. Although you should create a second user for the database and use that account instead of the postgres user once you've completed the initial setup. Hope that helps.
     

Share This Page