Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
883 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...