如何将 const char 连接到 const char?

发布于 2024-12-16 20:02:45 字数 1529 浏览 3 评论 0原文

我需要构建其他 const charconst char 字符串?

const char *sql = "";
const char *sqlBuild = "";

for(int i=0; i < ac_count; ++i) {

  if (![sqlBuild isEqualToString:@""]) {
     sqlBuild = [sqlBuild stringByAppendingString:
           [NSString stringWithUTF8String:@" UNION "]];
  }
  sql = [[NSString stringWithFormat:
      @"select sum(price) from tmp%d  where due >= date() and due <= '%@'",  
            i, strDBDate] cStringUsingEncoding:NSUTF8StringEncoding];

  sqlBuild = [sqlBuild stringByAppendingString:
       [NSString stringWithUTF8String:sql]];
}

//execute sql

我已经尝试过几次,但都不太正确,这是我的最后一次尝试。正如你所看到的,我正在尝试构建一个 sql 语句。

我哪里出错了?

编辑 - 我正在使用 sql lite,它不喜欢 NSString,请参见下文。

- (NSString*)getCategoryDesc:(int)pintCid {

NSString *ret = @"";
const char *sql = "select category from categories where cid = ?";

sqlite3 *database;
int result = sqlite3_open([[General getDBPath] UTF8String], &database);
if(result != SQLITE_OK)
{
    DLog(@"Could not open db.");
}

sqlite3_stmt *statementTMP;

int error_code = sqlite3_prepare_v2(database, sql, -1, &statementTMP, NULL);

if(error_code == SQLITE_OK) {

    sqlite3_bind_int(statementTMP, 1, pintCid);

    if (sqlite3_step(statementTMP) == SQLITE_ROW) {
        ret = [[NSString alloc] initWithUTF8String:
            (char *)sqlite3_column_text(statementTMP, 1)];
    }
}
sqlite3_finalize(statementTMP);
sqlite3_close(database);


return [ret autorelease];
}

I need to build up a const char string of other const char's ?

const char *sql = "";
const char *sqlBuild = "";

for(int i=0; i < ac_count; ++i) {

  if (![sqlBuild isEqualToString:@""]) {
     sqlBuild = [sqlBuild stringByAppendingString:
           [NSString stringWithUTF8String:@" UNION "]];
  }
  sql = [[NSString stringWithFormat:
      @"select sum(price) from tmp%d  where due >= date() and due <= '%@'",  
            i, strDBDate] cStringUsingEncoding:NSUTF8StringEncoding];

  sqlBuild = [sqlBuild stringByAppendingString:
       [NSString stringWithUTF8String:sql]];
}

//execute sql

I've had several attempts but can't get it quite right, heres my last attempt. As you can see i'm trying to build up an sql statement.

Where am I going wrong ?

EDIT - I'm using sql lite which doesn't like NSString, see below.

- (NSString*)getCategoryDesc:(int)pintCid {

NSString *ret = @"";
const char *sql = "select category from categories where cid = ?";

sqlite3 *database;
int result = sqlite3_open([[General getDBPath] UTF8String], &database);
if(result != SQLITE_OK)
{
    DLog(@"Could not open db.");
}

sqlite3_stmt *statementTMP;

int error_code = sqlite3_prepare_v2(database, sql, -1, &statementTMP, NULL);

if(error_code == SQLITE_OK) {

    sqlite3_bind_int(statementTMP, 1, pintCid);

    if (sqlite3_step(statementTMP) == SQLITE_ROW) {
        ret = [[NSString alloc] initWithUTF8String:
            (char *)sqlite3_column_text(statementTMP, 1)];
    }
}
sqlite3_finalize(statementTMP);
sqlite3_close(database);


return [ret autorelease];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸印温柔 2024-12-23 20:02:45

与 UTF8 字符串之间的转换没有任何好处;在 NSString 中完成所有操作并最后将其转换为 C 样式字符串会更有效。 @"d" 语法的计算结果也是一个对象,而不是 C 风格的字符串。

因此,您应该简化并更正您的代码:

NSMutableString *sqlStatement = [NSMutableString string];

for(int i=0; i < ac_count; ++i) {

  if ([sqlStatement length])  [sqlStatement appendString:@" UNION "];

  [sqlStatement appendFormat:
      @"select sum(price) from tmp%d  where due >= date() and due <= '%@'",  
            i, strDBDate];

}

// execute SQL string [sqlStatement UTF8String]

There's no benefit to the conversion to and then from a UTF8 string; it'd be much more efficient to do everything in NSString and convert it to a C-style string at the end. The @"d" syntax also evaluates to an object, not a C-style string.

So you should simplify and correct your code to:

NSMutableString *sqlStatement = [NSMutableString string];

for(int i=0; i < ac_count; ++i) {

  if ([sqlStatement length])  [sqlStatement appendString:@" UNION "];

  [sqlStatement appendFormat:
      @"select sum(price) from tmp%d  where due >= date() and due <= '%@'",  
            i, strDBDate];

}

// execute SQL string [sqlStatement UTF8String]
鹿港小镇 2024-12-23 20:02:45

sqlBuild 声明为 NSMutableString 并使用它来构建字符串:

NSMutableString *sqlBuild = [NSMutableString string];
for (...) {
    [sqlBuild appendString:...];
}

Declare sqlBuild to be NSMutableString and use that to build up your string:

NSMutableString *sqlBuild = [NSMutableString string];
for (...) {
    [sqlBuild appendString:...];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文