2016-07-27 37 views
0

你好有幫助的鄉親,JDBC拋出的MySQL語法異常,儘管有效的查詢

我在其中創建一個基於一個mysqldump的一個新的DB模式的應用的一小部分。 相關部分看起來是這樣的:

log.info("= setup basic database"); 
// Execute each command in the dump 
for (String query : dump.split(";")) { 
    statement.execute(query); 
} 

這工作正常,但一些語句只是一些像斷線

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 ''ot_subtotal.php' at line 1 

在這種情況下,查詢是

INSERT INTO `configuration` VALUES (194,'MODULE_ORDER_TOTAL_INSTALLED','ot_subtotal.php;ot_discount.php;ot_coupon.php;ot_shipping.php;ot_cod_fee.php;ot_gv.php;ot_subtotal_no_tax.php;ot_tax.php;ot_total_netto.php;ot_total.php',6,0,NULL,'2016-06-17 15:27:24',NULL,NULL); 

如果我更換字符串「'ot_subtotal.php; ot_discount.php; ot_coupon.php; ot_shipping.php; ot_cod_fee.php; ot_gv.php; ot_subtotal_no_tax.php; ot_tax.php; ot_total_netto.php ; ot_total.php'「與類似」hello world「的東西一起工作,稍後在另一個查詢中中斷。

事情是,我能夠通過終端命令「mysql ... < dump.sql」導入轉儲沒有問題。

任何想法?

回答

3

這是因爲

for (String query : dump.split(";")) { 
    statement.execute(query); 
} 

您在;這導致不完全分裂查詢的發生。

例如:

INSERT INTO `configuration` VALUES (194,'MODULE_ORDER_TOTAL_INSTALLED','ot_subtotal.php;ot_discount.php;ot_coupon.php;ot_shipping.php;ot_cod_fee.php;ot_gv.php;ot_subtotal_no_tax.php;ot_tax.php;ot_total_netto.php;ot_total.php',6,0,NULL,'2016-06-17 15:27:24',NULL,NULL); 

上述拆分後的查詢在;

INSERT INTO `configuration` VALUES (194,'MODULE_ORDER_TOTAL_INSTALLED','ot_subtotal.php 

這是不正確。

+0

哎喲,這麼明顯。感謝您的快速幫助=) – Finn

+0

很高興幫助你:) – Sanjeev

2

而不是

dump.split(";") 

使用

dump.split(";\n") 

如果你創建了正常的方式轉儲文件。

相關問題