为什么这段代码没有得到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