我使用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頭文件。