iphone - 插入带撇号的字符串值

标签 iphone objective-c sqlite

考虑一个需要插入到 SQLite 表中的带撇号的字符串。

INSERT INTO myTable ( table_Title TEXT ) VALUES ( 'world's' )

如何在 INSERT 语句的值中标记或转义撇号?

最佳答案

http://www.sqlite.org/c3ref/bind_blob.html

您不应该像这样将输入作为字符串直接传递到查询中。这不仅令人讨厌,而且还容易受到 SQL 注入(inject)攻击,这对于 Mac 应用程序来说可能不是什么大问题,但是当您可以使用 Prepared Statements 时,这仍然是一个坏主意。准备好的语句确保底层数据库安全地读取实际的、未修改的输入值。

对于您的具体示例(为简洁/清晰起见,删除了错误检查):

sqlite3 *db;
sqlite3_open("test.db", &db);

// Create a prepared statement
sqlite3_stmt *stmt;
const char *sql = "INSERT INTO myTable (table_Title TEXT) VALUES (?)";
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);

// Bind the parameter (safely)
sqlite3_bind_text(stmt, 1, "world's", -1, NULL);

// Execute the prepared statement
sqlite3_step(stmt);

(未经测试,但您明白了)。

关于iphone - 插入带撇号的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4230793/

相关文章:

iphone - iOS 应用程序的动态 localization.strings 定义?

iphone - 听iPhone通知吗?

c++ - NSPropertyListSerialization propertyListWithData 因二进制 plist 而失败

sql - SQLite 中的向量/列表

c# - SQLite 尝试写入只读数据库错误

android - Android Sqlite/ORMLite 中的十六进制字符串与大整型索引

iphone - 如何在 iphone 中创建具有 Marquee Effect 的动态多个 uiview

iphone - 如何在 iPhone 上解析未知格式的日期字符串?

ios - subview 在 iOS7 中没有接收到触摸

ios - 当我构建并运行一个空的ios应用时,xcode 5.0.1崩溃