how to store double quotation marks in sqlite using iOS

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

NSString* insertSQL = [NSString stringWithFormat: @"UPDATE tablename SET SummaryDescription=\"%@\" WHERE SummaryDate1=\"%@\" AND ClientId=\"%@\"",SummaryDescription,[_dic objectForKey:@"SummaryDate1"],[[NSUserDefaults standardUserDefaults]objectForKey:@"ClientID"]];

 [db updateTable:insertSQL];

In SummaryDescription contains value hai “Man”.But I cannot able to store this data into database.
If we remove this double quotation marks we can store this data.Is there is any other way to store these types of double quotation marks string.

  • How do I access SQLite database instance on iPhone?
  • store images into sqlite database
  • Use and Access Existing SQLite Database on iOS
  • Objective-C and sqlite's DATETIME type
  • How to implement “Load More Records” in TableView in iPhone SDK?
  • How to specify range with NSPredicate in Objective C
  • 3 Solutions Collect From Internet About “how to store double quotation marks in sqlite using iOS”

    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 🙂

    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"]];
    

    You need to Escape(/) the quotation mark.

    Like :

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