我对erlang很新,我需要编写一些在MySQL数据库中插入行的代码。 如何使用Erlang阻止SQL注入?是否还有其他语言的准备语句或我应该怎么做?
感谢您的回复。
答案 0 :(得分:6)
此答案取决于您使用的驱动程序。
Erlang ODBC有一个函数param_query,它将一组参数绑定到查询中,它也可以转义所有SQL特殊字符。
erlang-mysql-driver已准备好陈述:
%% Register a prepared statement
mysql:prepare(update_developer_country,
<<"UPDATE developer SET country=? where name like ?">>),
%% Execute the prepared statement
mysql:execute(p1, update_developer_country, [<<"Sweden">>,<<"%Wiger">>]),
(来自Yariv's blog的代码)
作为最后的手段,您可以随时escape the characters
NUL (0x00) --> \0
BS (0x08) --> \b
TAB (0x09) --> \t
LF (0x0a) --> \n
CR (0x0d) --> \r
SUB (0x1a) --> \z
" (0x22) --> \"
% (0x25) --> \%
' (0x27) --> \'
\ (0x5c) --> \\
_ (0x5f) --> \_