如何读取 groovy-sql 语句中的每一行?

How to read each row in a groovy-sql statement?

我正在尝试读取具有五行五列的 table。我使用 sql.eachRow 函数来读取 eachRow 并将值分配给 String。我遇到错误 "Groovy:[Static type checking] - No such property: MachineName for class: java.lang.Object"

我的代码:

sql.eachRow('select * from [MACHINES] WHERE UpdateTime > :lastTimeRead, [lastTimeRead: Long.parseLong(lastTimeRead)])
{ row ->
    def read = row.MachineName
}

但 MachineName 是我的列名。我怎样才能克服这个错误。

无法将动态属性与静态类型检查一起使用*。

然而,eachRow will pass a GroovyResultSet as first parameter to the Closure. This means that row has the type GroovyResultSet and so you can access the value using getAt

row.getAt('MachineName')

应该可以。在 groovy 中,您还可以使用 []-运算符:

row['MachineName']

等价于第一种解

*) 没有 type checking extension.

如果您知道列名,您可以使用下面的名称。

"$row.MachineName" 

但是如果您不知道列名或遇到一些问题,仍然可以使用 Select.

的数组访问它
sql.eachRow('select * from [MACHINES] WHERE UpdateTime > :lastTimeRead, [lastTimeRead: Long.parseLong(lastTimeRead)]) 
{ row->
        log.info "First value = ${row[0]}, next value = ${row[1]}"
}