Sqlite SQL格式化输入函数splite3_mprintf
生活随笔
收集整理的這篇文章主要介紹了
Sqlite SQL格式化输入函数splite3_mprintf
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
sqlite中,
在使用sql語句寫入字符串數據時, 由于字符數據中可能隱含轉義字符,
如果對于他們不作處理,執行時,exec函數將不識別,或者造成注入攻擊
這個時候sqlite_mprintf(),
應該就是必須使用了,配合‘%q’將字符數據中的轉義字符,直接轉換,
就不用擔心字符串中含有‘單引號,這樣造成sql語句不識別的問題。
For example, assume the string variable zText contains text as follows:
char *zText = "It's a happy day!";One can use this text in an SQL statement as follows:
char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText); sqlite3_exec(db, zSQL, 0, 0, 0); sqlite3_free(zSQL);Because the %q format string is used, the '\'' character in zText is escaped and the SQL generated is as follows:
INSERT INTO table1 VALUES('It''s a happy day!')This is correct. Had we used %s instead of %q, the generated SQL would have looked like this:
INSERT INTO table1 VALUES('It's a happy day!')總結
以上是生活随笔為你收集整理的Sqlite SQL格式化输入函数splite3_mprintf的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 招行白金卡积分兑换里程合作航空公司有哪些
- 下一篇: 定活两便和整存整取区别,体现在这几方面