我使用sqlite数据库在离线模式下存储值.在这些值中,它包含带双引号的字符串.(例如: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];
在SummaryDescription中包含值hai“Man”.但我无法将此数据存储到数据库中.
如果我们删除这个双引号我们可以存储这些数据.是否有任何其他方法来存储这些类型的双引号字符串.
解决方法
要存储双引号或任何其他特殊字符,请使用参数将字符串传递给sql语句(
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],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);
希望这会帮助你.快乐编码:)