官方说明:
https://dev.mysql.com/doc/refman/5.7/en/mysql-real-escape-string.html
相关资料:
https://dev.mysql.com/worklog/task/?id=8077
从MySQL 5.7.6版本开始,如果启用了NO_BACKSLASH_ESCAPES,
则mysql_real_escape_string()函数失败,错误码为CR_INSECURE_API_ERR,
这个时候应当使用mysql_real_escape_string_quote()替代mysql_real_escape_string()。
启用NO_BACKSLASH_ESCAPES,表示将反斜杠当作普通字符,而不是转义字符:
SET sql_mode='NO_BACKSLASH_ESCAPES';
查看当前的SQL模式:
mysql> SELECT @@sql_mode;
+--------------------------------------------+
| @@sql_mode |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
1 row in set (0.01 sec)
未启用NO_BACKSLASH_ESCAPES前(反斜杠为转义字符):
mysql> SELECT '\\'\G;
*************************** 1. row ***************************
\: \
1 row in set (0.00 sec)
在启用NO_BACKSLASH_ESCAPES后(反斜杠为普通字符):
mysql> SET sql_mode='NO_BACKSLASH_ESCAPES';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT '\\'\G;
*************************** 1. row ***************************
\\: \\
1 row in set (0.00 sec)
mysql> SELECT '\"'\G;
*************************** 1. row ***************************
\": \"
1 row in set (0.00 sec)
相关源代码:
#define CR_INSECURE_API_ERR 2062
测试代码():
try
{
// 未启用NO_BACKSLASH_ESCAPES
printf("%s\n", mysql.escape_string(argv[2]).c_str());
// 启用NO_BACKSLASH_ESCAPES
mysql.update("%s", "SET sql_mode='NO_BACKSLASH_ESCAPES'");
printf("%s\n", mysql.escape_string(argv[2]).c_str());
}
catch (mooon::sys::CDBException& ex)
{
fprintf(stderr, "%s\n", ex.str().c_str());
}
运行结果:
# ./mysql_escape_test 'root@127.0.0.1:3306' '0x1a'
MYSQL_SERVER_VERSION: 5.7.12
0x1a
db_exception://[2062]Insecure API function call: 'mysql_real_escape_string' Use instead: 'mysql_real_escape_string_quote'@/data/X/mooon/src/sys/mysql_db.cpp:166
mysql_real_escape_string_quote函数(MySQL 5.7.6引入):
unsigned long mysql_real_escape_string_quote(MYSQL *mysql, char *to, const char *from, unsigned long from_length, char quote);
返回值:被转后的长度
参数to:被转后的
参数from:被转前的
参数length:from的有效字符数,不包括结尾符
参数quote:需要处理的字符,如:\, ', ", NUL (ASCII 0), \n, \r
阅读(4361) | 评论(0) | 转发(0) |