博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sqlite3 转义
阅读量:4111 次
发布时间:2019-05-25

本文共 3557 字,大约阅读时间需要 11 分钟。

Formatted String Printing Functions

char *sqlite3_mprintf(const char*,...);char *sqlite3_vmprintf(const char*, va_list);char *sqlite3_snprintf(int,char*,const char*, ...);char *sqlite3_vsnprintf(int,char*,const char*, va_list);

These routines are work-alikes of the "printf()" family of functionsfrom the standard C library.

The sqlite3_mprintf() and sqlite3_vmprintf() routines write theirresults into memory obtained from .The strings returned by these two routines should bereleased by . Both routines return aNULL pointer if is unable to allocate enoughmemory to hold the resulting string.

The sqlite3_snprintf() routine is similar to "snprintf()" fromthe standard C library. The result is written into thebuffer supplied as the second parameter whose size is given bythe first parameter. Note that the order of thefirst two parameters is reversed from snprintf(). This is anhistorical accident that cannot be fixed without breakingbackwards compatibility. Note also that sqlite3_snprintf()returns a pointer to its buffer instead of the number ofcharacters actually written into the buffer. We admit thatthe number of characters written would be a more useful returnvalue but we cannot change the implementation of sqlite3_snprintf()now without breaking compatibility.

As long as the buffer size is greater than zero, sqlite3_snprintf()guarantees that the buffer is always zero-terminated. The firstparameter "n" is the total size of the buffer, including space forthe zero terminator. So the longest string that can be completelywritten will be n-1 characters.

The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().

These routines all implement some additional formattingoptions that are useful for constructing SQL statements.All of the usual printf() formatting options apply. In addition, thereis are "%q", "%Q", and "%z" options.

The %q option works like %s in that it substitutes a nul-terminatedstring from the argument list. But %q also doubles every '\'' character.%q is designed for use inside a string literal. By doubling each '\''character it escapes that character and allows it to be inserted intothe string.

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 zTextis 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 SQLwould have looked like this:

INSERT INTO table1 VALUES('It's a happy day!');

This second example is an SQL syntax error. As a general rule you shouldalways use %q instead of %s when inserting text into a string literal.

The %Q option works like %q except it also adds single quotes aroundthe outside of the total string. Additionally, if the parameter in theargument list is a NULL pointer, %Q substitutes the text "NULL" (withoutsingle quotes). So, for example, one could say:

char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);sqlite3_exec(db, zSQL, 0, 0, 0);sqlite3_free(zSQL);

The code above will render a correct SQL statement in the zSQLvariable even if the zText variable is a NULL pointer.

The "%z" formatting option works like "%s" but with theaddition that after the string has been read and copied intothe result, is called on the input string.

See also lists of , , and .

转载地址:http://isosi.baihongyu.com/

你可能感兴趣的文章
Subsets II
查看>>
Edit Distance 字符串距离(重重)
查看>>
Gray Code 格雷码
查看>>
对话周鸿袆:从程序员创业谈起
查看>>
web.py 0.3 新手指南 - 如何用Gmail发送邮件
查看>>
web.py 0.3 新手指南 - RESTful doctesting using app.request
查看>>
web.py 0.3 新手指南 - 使用db.query进行高级数据库查询
查看>>
web.py 0.3 新手指南 - 多数据库使用
查看>>
一步步开发 Spring MVC 应用
查看>>
python: extend (扩展) 与 append (追加) 的差别
查看>>
「译」在 python 中,如果 x 是 list,为什么 x += "ha" 可以运行,而 x = x + "ha" 却抛出异常呢?...
查看>>
谷歌阅读器将于2013年7月1日停止服务,博客订阅转移到邮箱
查看>>
浅谈JavaScript的语言特性
查看>>
LeetCode第39题思悟——组合总和(combination-sum)
查看>>
LeetCode第43题思悟——字符串相乘(multiply-strings)
查看>>
LeetCode第44题思悟——通配符匹配(wildcard-matching)
查看>>
LeetCode第45题思悟——跳跃游戏(jump-game-ii)
查看>>
LeetCode第46题思悟——全排列(permutations)
查看>>
LeetCode第47题思悟—— 全排列 II(permutations-ii)
查看>>
LeetCode第48题思悟——旋转图像(rotate-image)
查看>>