Objective-C and MySQL

前端 未结 1 1833
抹茶落季
抹茶落季 2021-02-09 16:40

I\'ve been able to connect to a MySQL database in my app and use the C API which is almost exactly like PHP commands (mysql_real_connect(), mysql_query(), mysql_fetch_array(), e

相关标签:
1条回答
  • 2021-02-09 16:52

    There's no Apple-supplied Objective-C API for MySQL. There are a few third-party wrappers of the C API, though. Take a look at the MySQL-Cocoa Framework, for example.

    Given your familiarity with the PHP and C API, it may be more straightforward for you simply to use the C API. You'll need to handle conversion between objects and C data types, but this isn't much work.

    Edit

    You're crashing because the row value returned by the mysql API isn't an object, and your format string is telling NSLog to treat it as one. The %@ is a format-string placeholder for an object, not a C data type.

    It's not clear what the value is in this case. The context seems to imply that it's image data. If that's the case, you'll likely want to create an NSData object from the blob returned by the query, e.g.:

    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 NSString's -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 ];
    
    0 讨论(0)
提交回复
热议问题