【发布时间】:2015-02-26 23:47:12
【问题描述】:
我遇到了一个似乎无法解决的 SQL 问题。问题是当使用 sql 语句本身使用的特殊字符时,它根本不喜欢它。我尝试了很多方法,但找不到答案。
错误是由于在放入 SQL 表的文本中使用了 ' 字符。
NobleCore.getSQLStaticly().updateSQL("INSERT INTO Punishment (`ID`, `UUID`, `PlayerName`, `StaffName`, `Sev`, `pType`, `Reason`, `Activated`, `Ends`, `Active`, `RemoveReason`, `RemovedBy`) VALUES (NULL, '" + playerUUID + "', '" + player + "', '" + staffName + "' , '" + severity + "', '" + pType +"', '" + reason + "', CURRENT_TIMESTAMP,'" + end + "', '1', NULL, NULL );");
正如你所看到的,当它从方法参数中添加 '" + reason + "' 字符串时,它会出错,导致语法异常,因为它包含 ' 字符。我怎样才能让它添加这个字符,以便它知道它是字符串的一部分,而不是在语句中使用。
[17:28:29] [Server thread/WARN]: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ']', CURRENT_TIMESTAMP,'2015-02-26 17:58:29.708', '1', NULL, NULL )' at line 1
如果您需要任何其他代码,请告诉我,但我不知道它会有什么帮助:P
【问题讨论】:
-
您应该在保存之前对 java 中的数据进行清理/转义。
-
变量reason的值是多少?