gpt4 book ai didi

objective-c - 将 C 数据类型转换为 objective-c 对象

转载 作者:太空宇宙 更新时间:2023-11-04 02:17:05 25 4
gpt4 key购买 nike

我正在使用 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 在您的项目中工作,您必须进行一些设置:

  1. 将两个框架文件 libmysqlclient.a 和 libmysqlclient_r.a(应该在/usr/local/mysql/lib 中)复制到您的项目中(将它们放在框架组/文件夹中)。当您将内容复制到项目中时,包括所有子文件夹或任何消息。

  2. 展开目标,右键单击然后选择添加 -> 添加新构建阶段 -> 新建复制文件构建阶段。将目标更改为框架。这会将这两个框架放入项目的构建中。

  3. 将/usr/local/mysql/include 文件夹复制到您的项目中。这些是 MySQL 头文件。

最佳答案

NSArray 没有“initWithString”方法。 .

可能你想要的是这样的……

NSString *s = [NSString alloc] initWithBytes: row[i] length: strlen(row[i]) encoding: DefaultCstring]

关于objective-c - 将 C 数据类型转换为 objective-c 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6061592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com