我正在尝试将数据插入 MySQL,但在这样做时遇到了输入有单引号的错误,因此破坏了我的 sql 插入字符串。
这是我的代码行
String visitorSql = "INSERT INTO visitor" + "(encryptedEmail)" + "VALUES ('" + encryptedEmail.replace("'", "\\'")+"')";
语法错误是,
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''w+8L8%]/ò“VZ(R?cêíµ–ö\')' at line 1
为了保护隐私,我对电子邮件进行了加密。任何其他想法我应该如何编码它以逃避报价?
最佳答案
您必须将所有单引号 (') 替换为 ''(2 个单引号) 以转义它们。
String visitorSql = "INSERT INTO visitor(COLUMN_NM) VALUES ('" + encryptedEmail.replace("'", "''")+"')";
关于java - 用反斜杠转义引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46556927/