简体   繁体   中英

Using Groovy Strings as SQL select statements

Database Mysql , version 5.5.17.JDBC driver version 5.1.28 Code with Gstring:

def p = "id"

def row = sql.firstRow("select $p from Model")

print row

result:

[id:id] **!**

Code without Gstring:

def row = sql.firstRow("select id from Model")

print row

result:

[id:1]

Why?

Groovy is escaping the p as it treats it as a Sql parameter

Try using Sql.expand like so:

def p = 'id'
def row = sql.firstRow( "select ${Sql.expand( p )} from Model" )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM