如何使用iOS在sqlite中存储双引号 [英] how to store double quotation marks in sqlite using iOS

查看:259
本文介绍了如何使用iOS在sqlite中存储双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用sqlite数据库在离线模式下存储值。在这些值中,它包含带双引号的字符串。(例如: haiMan

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

在SummaryDescription中包含值 haiMan 。但我无法将这些数据存储到数据库中。
如果我们删除这个双引号,我们可以存储这些数据。是否有任何其他方法来存储这些类型的双引号字符串。

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.

推荐答案

要存储双引号或任何其他特殊字符,请使用参数将字符串传递给SQL语句(按照CL的建议在这个答案中):

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

这篇关于如何使用iOS在sqlite中存储双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆