爲什麼這段代碼沒有得到sql.execute(「$ y」)的字符串?爲什麼這個錯誤發生在groovy sql jdbc builder中?
import groovy.sql.Sql
def sql = Sql.newInstance("jdbc:mysql://localhost", "root","password", "com.mysql.jdbc.Driver")
def y= "select * from table"
table(sql,y)
def table(sql,x){
println ("$x")
sql.execute("$x")
}
輸出:
'select * from table'
Sep 02, 2017 3:49:39 PM groovy.sql.Sql$AbstractQueryCommand execute
WARNING: Failed to execute: ? because: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''select * from table'' at line 1
Caught: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''select * from table'' at line 1
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''select * from table'' at line 1
「double」撇號在groovy中有宏指令,'single'not –
TABLE是一個保留字...需要分隔。 (MySQL使用back-ticks。)但是,甚至更好,重命名錶。 – jarlh