mysql - 使用 Groovy 字符串作为 SQL select 语句

标签 mysql groovy

数据库 Mysql ,版本 5.5.17.JDBC 驱动程序版本 5.1.28 使用 Gstring 的代码:

def p = "id"

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

print row

结果:

[id:id] **!**

没有 Gstring 的代码:

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

print row

结果:

[id:1]

为什么?

最佳答案

Groovy 正在转义 p,因为它将其视为 Sql 参数

尝试使用Sql.expand像这样:

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

关于mysql - 使用 Groovy 字符串作为 SQL select 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21576852/

相关文章:

mysql - 标签系统的最佳模型

regex - 如何使用 Groovy 的 replaceFirst 和闭包?

mysql - Spring Batch JpaPagingItemReader(可能)在使用 SELECT native 查询时从数据库中删除行

sorting - Groovy 自定义按值对 map 进行排序

mysql - 如何通过 Groovy sql 字符串调用 SQL 的 now()?

MySQL 十进制说明

mysql - 根据跨多行的约束选择行

mysql - native 表 'performance_schema' .'???' 结构错误

sql - 在 Gradle 任务中执行 SQL?

PHP for-loop with MySQL 行取决于 MySQL 查询结果