Objective-C和MySQL [英] Objective-C and MySQL

查看:129
本文介绍了Objective-C和MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够连接到我的应用程序中的MySQL数据库并使用C API,它几乎完全类似于PHP命令(mysql_real_connect(),mysql_query(),mysql_fetch_array()等),而且我很漂亮随便我只是不确定如何返回数据查询.我要使用数组还是字典,然后如何解析它.例如,在PHP中,我会做类似的事情(在连接之后):

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(), etc.) and which I am pretty comfortable with I'm just not sure how the data query is returned. Do I use an array or dictionary and then how would I parse it. For example, in PHP I would do something like so (after the connection):

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

与Objective-C等效的东西是什么?谢谢.

What would the objective-c equivalent be? Thanks.

好的,所以我取得了一些进步-我可以进行查询并获取返回的字段/行数,但似乎无法访问数据本身.这是我的代码,是我从MySQL文档和其他一些站点中拼凑而成的:

OK, so I made some progress - I can make the query and get the number of fields/rows returned, just can't seem to access the data itself. Here's my code, which I stitched together from the MySQL docs and a few 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];
}

推荐答案

Apple没有提供MySQL的Objective-C API.不过,有一些C API的第三方包装器.看看 MySQL-Cocoa框架,例如.

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.

鉴于您对PHP和C API的熟悉,简单地使用C API可能会更直接.您需要处理对象和C数据类型之间的转换,但这没什么用.

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.

修改

您崩溃了,因为mysql API返回的行值不是对象,并且您的格式字符串告诉NSLog将其视为一个. %@是对象的格式字符串占位符,而不是C数据类型.

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.

目前尚不清楚该值是什么.上下文似乎暗示它是图像数据.如果是这种情况,您可能想从查询返回的Blob中创建一个NSData对象,例如:

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 ];

如果结果字段只是字符串,请使用NSString-initWithBytes:length:encoding:方法:

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 ];

这篇关于Objective-C和MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆