1

我正在使用 mySql 目标 c 库:http : //www.karlkraft.com/index.php/2010/09/17/mysql-for-iphone-and-osx/ 并尝试在 LIKE 语句中使用西里尔符号:

NSString *cmd = [NSString stringWithFormat:@"select id,qrId,name,surname from users where name like '%%%@%%'",_nameField.text];
    userFetch = [MysqlFetch fetchWithCommand:cmd onConnection:myConnection];

它适用于英文字符,但在 _nameField.text 中使用西里尔符号我收到此错误: *由于未捕获的异常“MysqlException”而终止应用程序,原因:“无法执行 mysql_stmt_bind_result() 错误 #1064:您的 SQL 中有错误句法; 检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ''%–∞%' 附近使用正确的语法

在 MysqlFetch.m 中,我的命令被转换为 UT8String,这会导致 mysql_stmt_prepare 出现异常:

if (mysql_stmt_prepare(myStatement, [s UTF8String],[s length])) {
  [MysqlException raiseConnection:connection
                       withFormat:@"Could not perform mysql_stmt_bind_result() Error #%d:%s"
   ,mysql_errno(connection.connection),
   mysql_error(connection.connection)];
}

我可以使用 MysqlInsert 使用 cyrylic NSStrings 进行插入。它不会导致异常,因为没有字段值(可能包含西里尔符号),只有字段名称:

NSArray *keys = [rowData allKeys];

NSMutableString *cmd = [NSMutableString string];
[cmd appendFormat:@"INSERT INTO %@ ( ",table];
BOOL firstAdd=YES;
for (NSString *columnName in keys) {
  if (firstAdd) {
    [cmd appendFormat:@" %@ ",[columnName mysqlEscapeInConnection:connection]];
    firstAdd=NO;
  } else {
    [cmd appendFormat:@", %@ ",[columnName mysqlEscapeInConnection:connection]];
  }
}

[cmd appendFormat:@" ) values ( ",table];

firstAdd=YES;
for (NSString *columnName in keys) {
  if (firstAdd) {
    [cmd appendString:@" ? "];
    firstAdd=NO;
  } else {
    [cmd appendString:@", ? "];
  }
}
[cmd appendFormat:@" ) "];

MYSQL_STMT *myStatement = mysql_stmt_init(connection.connection);

if (mysql_stmt_prepare(myStatement, [cmd UTF8String],[cmd length])) {
  [MysqlException raiseConnection:connection withFormat: @"mysql_stmt_prepare failed %@ %s",cmd, mysql_stmt_error(myStatement)];
}

稍后添加值:

NSObject *object = [rowData objectForKey:key];
  if ([object isKindOfClass:[NSString class]]) {
    NSString *s = (NSString *)object;
    const char *ch = [s UTF8String];
    binding[x].is_null= 0;
    binding[x].buffer_type = MYSQL_TYPE_STRING; 
    binding[x].buffer = (void *)ch;
    binding[x].buffer_length= strlen(ch);        
  }

可能有太多的文字和简单的问题:如何将西里尔文 NSString 转换为与 MysqlUpdate 一起使用?

4

0 回答 0