用撇号插入字符串值

Beo*_*eok 6 sqlite iphone objective-c

考虑一个带有撇号的字符串,需要将其插入到SQLite表中.

INSERT INTO myTable ( table_Title TEXT ) VALUES ( 'world's' )
Run Code Online (Sandbox Code Playgroud)

如何在INSERT声明中的值中标记或转义撇号?

d11*_*wtq 16

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

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

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

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);
Run Code Online (Sandbox Code Playgroud)

(未经测试,但你明白了).