Thursday, November 11, 2010

Connecting to MySQL Through the Iphone Hell


Connecting to MySQL Through the Iphone Hell

So for the past few hours ive tried finding a way to get objective c/cocoa framework to connect to a database for my software engineering project. at first i thought i was going to have to use a sqllite database, but after a lot of reading it seems like thats not the answer. sqllite databases on the iphone are just locally stored databases on the phone itself, so it serves little purpose. so after a lot of reading it appears that cocoa doesn't have a direct way to connect to mysql, i don't know why apple wouldn't build this in?

after some more reading though, i stumbled across an open source cocoa mysql framework that uses c's myql library to connect to dbs. its located at http://sourceforge.net/projects/mysql-cocoa. after looking through the website, there are numerous releases of it and little documentation on which framework you should actually use. i finally found the right one and added it to a test database project. adding the framework to a project is rather tedious, but i finally got it to work.
the code to connect to a mysql database is fairly easy, about the same as php. the following code is suppose to connect to a database.

MCPConnection *theConnec;
MCPResult *theRes;
//initialize connection string vars
NSString *dbURL = @"XXXXXX";
NSString *userName = @"XXXXXX";
NSString *pass = @"XXXXXX";
//open connection to database
theConnec = [theConnec initToHost:dbURL withLogin:userName password:pass usingPort:3306];
//NSLog(@"The connection to database was successfull");
[theConnec selectDB:@"XXXXXX"];
//{
// NSLog(@"Database found");
//}
//else
//{
// NSLog(@"Database not found");
//}
theRes = [theConnec queryString:@"select * from seahawk_tag"];
//get the number of rows
NSInteger numberOfRows = [theRes numofRows];

NSLog(@"Query of MySQL Database %@", numberOfRows);
return NSApplicationMain(argc, (const char **) argv);
[theConnec release];

The previous code should simply connect to a database and pull in a simple table into the variable theRes, but for some reason it does nothing. I figured that i should allocate the connection object as the documentation suggests, but if i try and allocate it, the compiler just throws a very weird error. Also there isn't any real way to find the actual problem or see if the connection variable theConnec actually connects to the database. There is another method of the theConnec variable, connectWithHost or something of that nature, but it asks for a unix socket and a few other things, so its useless as well. hopefully ill be able to dissect the problem in the next few days......

a follow up this post, the easieset way ive found to query a remote mysql database is to have a php script do all of the querying for the application. this way all of the processing is done by the php instead of the iphone. so all you have to do is simply put out a say a login script at www.domain.com/login.php. Then just ping the website with something like www.domain.com/login.php?user=joe&password=test and if the user is found then return a 1 or return a 0 for not found. To ping the website the easiest and fastest way is to instantiate a string object with a url's source code by something like NSString *login = [init withUrl:www.domain.com/login.php?user=joe&password=test];. Thats probably not the write syntax but its close to it, just google for the correct syntax.

No comments:

Post a Comment

Followers