用单引号替换双引号以存储在SQLite中

时间:2012-09-12 09:10:17

标签: iphone objective-c sqlite xcode4.3

我想用字符串中的单引号替换所有双引号。我尝试过使用stringByReplacingOccurrencesOfString,但它不起作用。如何使用单引号替换所有双引号,以便将其存储到数据库中?

sqlite3_stmt    *statement;

NSString *string = [NSString stringWithString:charDesc.text];
[string stringByReplacingOccurrencesOfString:@"\"" withString:@"''"];

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK){
    NSString *insertSQL = [NSString stringWithFormat: @"UPDATE CHARACTER SET charName = \"%@\", charDescription = \"%@\" WHERE charID = %i", charName.text, charDesc.text, charID];

    const char *insert_stmt = [insertSQL UTF8String];

    sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);
    if (sqlite3_step(statement) == SQLITE_DONE){
        status.text = @"Succesfully Updated!";
    }else{
        status.text = @"Failed to Update!";
    }

    sqlite3_finalize(statement);
    sqlite3_close(database); 
}

1 个答案:

答案 0 :(得分:1)

string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"'"];