如何将 const char 连接到 const char?
我需要构建其他 const char
的 const 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与 UTF8 字符串之间的转换没有任何好处;在 NSString 中完成所有操作并最后将其转换为 C 样式字符串会更有效。
@"d"
语法的计算结果也是一个对象,而不是 C 风格的字符串。因此,您应该简化并更正您的代码:
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:
将
sqlBuild
声明为NSMutableString
并使用它来构建字符串:Declare
sqlBuild
to beNSMutableString
and use that to build up your string: