Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 18, 2011, 11:30 AM   #1
RIRedinPA
macrumors newbie
 
Join Date: May 2011
Cocoa and MySQL - parsing data...

In my app I am able to connect to a MySQL db and perform a query but I am having problems parsing the returned data. Here's my code:

Code:
- (IBAction)dbConnect:(id)sender {
    
	
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	MYSQL conn;
	mysql_init(&conn);
	
	if (!mysql_real_connect(&conn, "10.1.1.99", "*******", "******", "oldphotoarchive", 0, NULL, 0)) {
		NSLog(@"%@", [NSString stringWithUTF8String:mysql_error(&conn)]);
	} else { 
		int results = mysql_query(&conn, "SELECT * FROM photorecord");
		if (results == 0)  { 
			
			MYSQL_ROW row;
			unsigned int num_fields;
			unsigned int i;
			
			num_fields = mysql_num_fields(results);

			NSLog(@"%i", num_fields);
		}
	}
	
	[pool release];
}
I keep getting an warning that num_fields makes a pointer from integer without a cast, but wouldn't mysql_num_fields() return an integer? And let me take this moment to whine about the lack of a mysql_fetch_array or mysql_fetch_assoc in the C API...

Rereading my question should I be assigning results to something other than an int?
RIRedinPA is offline   0 Reply With Quote
Old May 18, 2011, 11:36 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
I know absolutely nothing about MySQL. So this is all based on reading their manual. I am assuming you did the same.

The signature for this function is
Code:
unsigned int mysql_num_fields(MYSQL_RES *result)
So it does return an unsigned int. As expected. It also expects to be passed a pointer to a MYSQL_RES (which I assume is a structure).

Your code is
Code:
int results = mysql_query(&conn, "SELECT * FROM photorecord");
...
num_fields = mysql_num_fields(results);
So, quite obviously, your results variable is of the wrong type. This may well compile and actually might well work on some platforms (where pointers have the same size as ints) but it's to be avoided. It'd take a good look at the example in the manual page I linked to and correct your code.
robbieduncan is offline   0 Reply With Quote
Old May 18, 2011, 03:52 PM   #3
KnightWRX
macrumors Pentium
 
KnightWRX's Avatar
 
Join Date: Jan 2009
Location: Quebec, Canada
You're missing a call in there. You can't take the return value from mysql_query() and feed it into mysql_num_fields(). mysql_query() returns an int, which is simply an indication of whether your query succeeded or failed. mysql_num_fields actually wants a structure pointer to the results themselves. You need to fetch those first with mysql_store_result().

Have you read the C API reference ?

http://dev.mysql.com/doc/refman/5.6/...um-fields.html
__________________
"What you leave behind is not what is engraved in stone monuments, but what is woven into the lives of others."
-- Pericles
KnightWRX is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Importing Data from a MySQL Database Futhark iPhone/iPad Programming 2 Jan 21, 2014 01:14 AM
RSS parsing help (simple) johnnyfla123 iPhone/iPad Programming 12 Nov 29, 2013 10:16 AM
parsing and displaying in tableview jennylcf iPhone/iPad Programming 2 Jan 28, 2013 06:31 AM
parsing XML in iphone? hajira iPhone 0 Sep 24, 2012 04:51 AM

Forum Jump

All times are GMT -5. The time now is 06:42 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC