作者:失心人2702939300 | 来源:互联网 | 2023-05-18 07:02
IwanttoaddastringtomysqldatabaseusingJDBC.Buttheproblemisthatwheneverthestringco
I want to add a string to my sql database using JDBC. But the problem is that whenever the string contains a double quote, then the sql command is interpreted completely differently and a "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax" gets thrown."
我想使用JDBC向sql数据库添加一个字符串。但问题是,每当字符串包含双引号时,sql命令的解释就会完全不同,“sql语法中有错误”;检查与MySQL服务器版本对应的手册,找到正确的语法“被抛出”。
For example, String ssql = "INSERT INTO tableName VALUES (\""+ string + "\")";
例如,String ssql = "插入到tableName值(\" \" + String + "\" \")";
if string = "abc", then sql = INSERT INTO tableName VALUES ("abc")
But if string = "ab\"cd", then sql = INSERT INTO tableName VALUES ("ab"c")
And hence for a string that contains a double quote, the sql command is interpreted completely differently.
如果字符串= "abc",则sql =插入到tableName值("abc")中,但如果字符串= "ab\"cd",则sql =插入到tableName值("ab"c")中,因此对于包含双引号的字符串,sql命令的解释完全不同。
How can I add such a string to the database.
如何向数据库中添加这样的字符串。
PS. I cannot afford to change the double quote to a single quote. And there can be other hacks to add such a string but I want to know if there really is no such way of adding such a string directly.
我不能把双重报价改为单一报价。还有其他方法可以添加这样的字符串但我想知道是否真的没有这种直接添加这样的字符串的方法。
5 个解决方案