[英]insert string value with apostrophe
考慮一個帶有撇號的字符串,需要將其插入到SQLite表中。
INSERT INTO myTable ( table_Title TEXT ) VALUES ( 'world's' )
如何在INSERT
語句中的值中標記或轉義撇號?
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);
(未經測試,但你明白了)。
您可以使用''而不是單個撇號。
插入的示例沒有
Insert into table_name values('haven''t');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.