Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store double quotation marks in sqlite using iOS

Tags:

sqlite

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.

like image 388
Kiran P Nair Avatar asked Sep 17 '26 02:09

Kiran P Nair


2 Answers

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 :)

like image 125
Shubhendu Avatar answered Sep 18 '26 15:09

Shubhendu


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"]];
like image 29
MMiroslav Avatar answered Sep 18 '26 15:09

MMiroslav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!