how to store double quotation marks in sqlite using iOS

后端 未结 3 1837
醉酒成梦
醉酒成梦 2020-12-12 00:45

I use sqlite data base to store values in offline mode.In these values it contains strings with double quotation marks.(eg: hai \"Man\")

         


        
相关标签:
3条回答
  • 2020-12-12 00:50

    You can replace all double quote occurrences in SummaryDescription calling this:

    NSString *newSummaryDescription = [SummaryDescription stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    

    So you code will look like this:

    NSString *newSummaryDescription = [SummaryDescription stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *insertSQL = [NSString stringWithFormat: @"UPDATE tablename SET SummaryDescription=\"%@\" WHERE SummaryDate1=\"%@\" AND ClientId=\"%@\"", newSummaryDescription, [_dic objectForKey:@"SummaryDate1"], [[NSUserDefaults standardUserDefaults] objectForKey:@"ClientID"]];
    
    0 讨论(0)
  • 2020-12-12 00:52

    To store double quotation marks or any other special character, use a parameter to pass the string into the SQL statement (as suggested by CL in this answer):

    NSString *str = @"some characters \" and \'";
    const char *sql = "INSERT INTO MyTable(Name) VALUES(?)";
    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
        sqlite3_bind_text(stmt, 1, [str UTF8String], -1, SQLITE_TRANSIENT);
        if (sqlite3_step(stmt) != SQLITE_DONE) {
            NSLog(@"SQL execution failed: %s", sqlite3_errmsg(db));
        }
    } else {
        NSLog(@"SQL prepare failed: %s", sqlite3_errmsg(db));
    }
    sqlite3_finalize(stmt);
    

    Hope this will help you. Happy Coding :)

    0 讨论(0)
  • 2020-12-12 00:58

    You need to Escape(/) the quotation mark.

    Like :

    hai "Man" : >> To >> : hai \""Man\""

    0 讨论(0)
提交回复
热议问题