Nikhil Sharma
Nikhil Sharma

Reputation: 2301

Gsql not executing while constructing query

I want to fire an update query , normally using groovy we do something like :

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

The above works perfectly but my problem is that i dont know ahead how many parameters i need to update. So i made a function which returns the properttoudate1 = value1 , propertytoupdate2 = value2 .. and so on.. Then i modified the above query to

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

Now it gives me the error::

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

ANY IDEAS ??

Upvotes: 1

Views: 142

Answers (1)

tim_yates
tim_yates

Reputation: 171084

You need to tell Groovy that the fieldname is a static, and shouldnt be replaced with ? by using Sql.expand:

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

Upvotes: 1

Related Questions