构造查询时Gsql不执行

时间:2011-10-17 10:41:45

标签: sql grails groovy

我想触发更新查询,通常使用groovy,我们会执行以下操作:

sql.executeUpdate("update MYTABLE l set field1  where l.id = ${someobj.id}")

以上工作完美但我的问题是我不知道需要更新多少参数。所以我做了一个返回的函数 properttoudate1 = value1,propertytoupdate2 = value2 ..等等.. 然后我将上面的查询修改为

sql.executeUpdate("update MYTABLE l set ${makeQueryString(propertiesToUpdate)} where l.id = ${someobj.id}")

现在它给了我错误::

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 ''PROPERTY1 = 1' where l.id = 'PROPERTVALUE1'' at line 1

任何想法??

1 个答案:

答案 0 :(得分:1)

您需要告诉Groovy该字段名是静态的,不应该被? by using Sql.expand替换:

sql.executeUpdate("update MYTABLE l set ${Sql.expand(makeQueryString(propertiesToUpdate))} where l.id = ${someobj.id}")