赞
踩
sqlite3数据库在搜索的时候,一些特殊的字符需要进行转义, 具体的转义如下:
/ -> //
' -> ''
[ -> /[
] -> /]
% -> /%
& -> /&
_ -> /_
( -> /(
) -> /)
c/c++ 转换函数
void replace_str(std::string& str, const std::string& before, const std::string& after)
{
for (std::string::size_type pos(0); pos != std::string::npos; pos += after.length())
{
pos = str.find(before, pos);
if (pos != std::string::npos)
str.replace(pos, before.length(), after);
else
break;
}
}
std::string sqliteEscape(std::string keyWord){
replace_str(keyWord,"/", "//");
replace_str(keyWord, "'", "''");
replace_str(keyWord, "[", "/[");
replace_str(keyWord, "]", "/]");
replace_str(keyWord, "%", "/%");
replace_str(keyWord, "&", "/&");
replace_str(keyWord, "_", "/_");
replace_str(keyWord, "(", "/(");
replace_str(keyWord, ")", "/)");
return keyWord;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。