将C数据类型转换为Objective-c对象
问题描述:
我使用c api从mysql数据库获取数据。我可以记录数据,所以我知道查询工作正常,但我需要把它放到字典中,而谷歌没有帮助。任何人都可以给我一个指针,片段或链接让我朝着正确的方向前进吗?将C数据类型转换为Objective-c对象
- (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);
lengths = mysql_fetch_lengths(result);
for(int i = 0; i < num_fields; i++) {
printf("[%.*s] ", (int) lengths[i], row[i] ? row[i] : "NULL");
if (row[i]) {
//AT THIS POINT I WANT TO DO THE CONVERSION, THIS ISN'T WORKING, INCOMPATIBLE POINTER TYPE
NSArray* thisRow = [[NSArray alloc] initWithString: row[i]];
}
}
printf("\n");
}
} 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];
}
这是修改的代码,以防其他人跑到同一堵墙中。随意批评:
#import "AppController.h"
#include "mysql.h"
#include "unistd.h"
@implementation AppController
- (IBAction)dbConnect:(id)sender {
NSMutableDictionary* results = [[NSMutableDictionary alloc] init];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MYSQL mysql;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql, "10.1.1.99", "mysqladmin", "m3r!0n", "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;
NSString* itemID = [[NSString alloc] init];
if (mysql_query(&mysql,"SELECT * FROM photorecord WHERE logNum > 10000")) {
// 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))) {
NSMutableDictionary* tmpDict = [[NSMutableDictionary alloc] init];
for(int i = 0; i < num_fields; i++) {
if (row[i]) {
char* cString = row[i];
NSString* dataString = [NSString stringWithCString:cString encoding:NSUTF8StringEncoding];
if ([dataString isNotEqualTo: @"NULL"]) {
//NSLog(@"%@", dataString);
if (i==0) {
itemID = dataString;
}
[tmpDict setObject: dataString forKey: [NSString stringWithFormat: @"item_%i", i] ];
}
}
}
[results setObject:tmpDict forKey:itemID];
NSLog(@"%@", results);
}
} 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];
}
@end
你必须做的,为了让MySQL在您的项目工作,成立了一个小点点:
复制两个框架文件libmysqlclient.a和libmysqlclient_r .a(应该在/ usr/local/mysql/lib中)添加到项目中(将它们放在框架组/文件夹中)。包括所有子文件夹或将东西复制到项目时的消息。
展开目标,右键单击,然后选择添加 - >添加新生成阶段 - >新建复制文件生成阶段。将目标更改为框架。这将这两个框架放入您的项目的构建中。
将/ usr/local/mysql/include文件夹复制到您的项目中。这些是MySQL头文件。
答
NSArray没有'initWithString'方法。 。
也许你想要的是这样的事情...
NSString *s = [NSString alloc] initWithBytes: row[i] length: strlen(row[i]) encoding: DefaultCstring]
答
NSArray获取从NSObject继承的项目。你可以编写一个包含MYSQL_ROW的单个成员的简单包装类,并将该对象传递给NSArray。
或者我认为你可以将MYSQL_ROW传递给NSString(可以从char *开始),然后将NSString传递给NSArray。