mysql - Using Groovy's ${...} with SQL -
i have question using string in jdbc sql query. here 2 examples , expect both work, don't.
working version ...
tabl = "action" query = "show full columns `action`;" println " "+ query dbconnection.eachrow( query ){ in error variant:
tabl = "action" query = "show full columns `${tabl}`;" println " "+ query dbconnection.eachrow( query ){ the error comes sql syntax error. can see statements textually identical.
the output show statement, error:
show full columns `action`; may 20, 2013 10:52:01 groovy.sql.sql eachrow warning: failed execute: show full columns `?`; because: parameter index out of range (1 > number of parameters, 0). may 20, 2013 10:52:01 groovy.sql.sql eachrow i think that's groovy trying culprit. when feed literal string jdbc connection works fine 'action' table.
i'm hoping can explain error , offer fix.
for reading, found option workaround:
query = "show full columns `"+ tabl.tostring() +"`;" while there might less verbose option, using "+"; me feels if using ${tabl} should work.
thanks in advance,
i think problem when use in-string variable produces different object type when don't use it. 1 string, other gstring.
e.g., see script , output:
def = "123" def b = "abc"+a def c = "abc${a}" println b.class println c.class >> class java.lang.string class org.codehaus.groovy.runtime.gstringimpl it seems eachrow function sensitive difference reason. 2 functions you're using are
- http://groovy.codehaus.org/api/groovy/sql/sql.html#eachrow(groovy.lang.gstring,%20groovy.lang.closure)
- http://groovy.codehaus.org/api/groovy/sql/sql.html#eachrow(java.lang.string,%20groovy.lang.closure)
but can't see why they'd behave differently..
another workaround call tostring on query variable - cast string:
def d = c.tostring() println d.class >> class java.lang.string
Comments
Post a Comment