Objective-C and MySQL

I managed to connect to the MySQL database in my application and use the C API, which is almost exactly similar to the PHP commands (mysql_real_connect (), mysql_query (), mysql_fetch_array (), etc.) and which I pretty I just don't know how to return a data request. I use an array or a dictionary, and then how to parse it. For example, in PHP, I would do something like this (after connecting):

$results = mysql_query("SELECT * FROM theDatabase"); if (mysql_num_rows($results) > 0) { while($row = mysql_fetch_array($results)) { print $row; } } 

What would be the equivalent of objective-c? Thanks.

Edit:

OK, so I made some progress - I can make a request and get the number of fields / rows returned, I just can’t access the data itself. Here is my code that I sewed along with the MySQL docs and several other sites:

 - (IBAction)dbConnect:(id)sender { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; MYSQL mysql; mysql_init(&mysql); if (!mysql_real_connect(&mysql, "10.1.1.99", "******", "******", "oldphotoarchive", 0, NULL, 0)) { NSLog(@"%@", [NSString stringWithUTF8String:mysql_error(&mysql)]); } else { MYSQL_RES *result; MYSQL_ROW row; unsigned int num_fields; unsigned int num_rows; unsigned long *lengths; if (mysql_query(&mysql,"SELECT * FROM photorecord")) { // error } else { // query succeeded, process any data returned by it result = mysql_store_result(&mysql); if (result) { num_fields = mysql_num_fields(result); while ((row = mysql_fetch_row(result))) { lengths = mysql_fetch_lengths(result); for(int i = 0; i < num_fields; i++) { //the line below is my problem, printing row[i] fails, I get the GNU gdb error... row[i] ? NSLog(@"%@", row[i]) : NSLog(@"wtf"); } } } else {// mysql_store_result() returned nothing; should it have? if (mysql_errno(&mysql)) { NSLog(@ "Error: %s\n", mysql_error(&mysql)); } else if (mysql_field_count(&mysql) == 0) { // query does not return data // (it was not a SELECT) num_rows = mysql_affected_rows(&mysql); } } } } [pool release]; } 
+6
source share
1 answer

There is no Objective-C API for Apple in MySQL. However, there are several third-party API C shells. Take a look at the MySQL-Cocoa Framework , for example.

Given your familiarity with the PHP and C API, it’s just easier for you to use the C API. You need to handle the conversion between objects and C data types, but that's not much.

Edit

You crash because the string value returned by the mysql API is not an object, and your format string tells NSLog treat it as one. %@ is the placeholder for the format string for the object, not the data type C.

It is not clear what this value is. Context seems to imply that this is image data. In this case, you most likely want to create an NSData object from the blob returned by the request, for example:

 NSData *imageData; imageData = [[ NSData alloc ] initWithBytes: row[ i ] length: lengths[ i ]]; NSLog( @"imageData: %@", imageData ); /* ...create NSImage, CGImage, etc... */ [ imageData release ]; 

If your result fields are just strings, use the NSString -initWithBytes:length:encoding: method

 NSString *s; s = [[ NSString alloc ] initWithBytes: row[ i ] length: lengths[ i ] encoding: NSUTF8StringEncoding ]; NSLog( @"result column %d: %@", i, s ); [ s release ]; 
+4
source

Source: https://habr.com/ru/post/888432/


All Articles