我使用 C api从mysql数据库获取数据。我可以记录数据,所以我知道查询工作正常,但我需要把它拿到字典中并且Google对此没有帮助。任何人都可以给我一个指针,片段或链接,让我朝着正确的方向前进吗?
- (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头文件。
答案 0 :(得分:2)
NSArray没有'initWithString'方法。 。
你想要的可能是这样......
NSString *s = [NSString alloc] initWithBytes: row[i] length: strlen(row[i]) encoding: DefaultCstring]
答案 1 :(得分:1)
NSArray接受从NSObject继承的项目。您可以编写一个包含MYSQL_ROW的单个成员的简单包装类,并将该对象传递给NSArray。
或者我认为您可以将MYSQL_ROW传递给NSString(可以从char *初始化),然后将NSString传递给NSArray。