sqlgroovygroovy-sql

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


I am trying to read a table having five rows and columns. I have used sql.eachRow function to read eachRow and assign the value to a String. I am getting an error "Groovy:[Static type checking] - No such property: MachineName for class: java.lang.Object"

My code:

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

but MachineName is my column name. How can i overcome this error.


Solution

  • Using dynamic Properties with static type checking is not possible*.

    However, 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')
    

    should work. In groovy you can also use the []-operator:

    row['MachineName']
    

    which is equivalent to the first solution.

    *) without a type checking extension.