我正在使用电子邮件地址查询数据库。地址就像fred.o'neill@smxi.com
在德尔福看起来像'fred.o''neill@smxi.com',因为撇号当然是逃脱的。
我们说查询是
'select * from table where (email = ''%s'')'
现在,如果我只是替换%s,则查询失败,因为它似乎将值视为2字符串'fred.o'和'neill@smxi.com'。即它不承认逃脱。
如果查询是
'select * from table where email = :email'
如果没有撇号,则参数设置为其工作的电子邮件地址,但如果电子邮件地址包含一个,则参数与数据不匹配
[编辑删除不正确的数据]
有什么建议吗?
答案 0 :(得分:7)
使用参数时,请不要自行添加引号或转义任何内容。
这应该有效:
var
email: String;
begin
email := 'fred.o''neill@smxi.com';
MyQuery.SQL.Text := 'SELECT * FROM table WHERE email = :email';
// Ensure that ParamCheck is enabled
MyQuery.ParamCheck := True;
// Explicitly set the type to string to ensure it's escaped
// E.g. binary types are not escaped
MyQuery.Params.ParamByName('email').DataType := ftString;
MyQuery.Params.ParamByName('email').Value := email;
MyQuery.Active := True;
end;
如果它没有返回任何内容,请检查存储在数据库中的实际值。插入时可能没有正确转义。