在Delphi中,使用QuotedStr函数可以防止SQL注入。QuotedStr函数将传入的字符串添加引号,防止特殊字符被解释为SQL语句的一部分。
例如,下面是一个使用QuotedStr的示例:
varusername: string;password: string;query: string;begin// 获取用户输入的用户名和密码username := EditUsername.Text;password := EditPassword.Text;// 构建查询语句,并使用QuotedStr函数处理用户名和密码query := 'SELECT * FROM users WHERE username = ' + QuotedStr(username) +' AND password = ' + QuotedStr(password);// 执行查询等后续操作// ...end;在上面的示例中,使用QuotedStr函数处理了用户输入的用户名和密码,确保它们被添加引号并准确地传递给SQL查询语句,从而防止SQL注入攻击。

