user9630935
user9630935

Reputation: 369

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.

Upvotes: 1

Views: 2852

Answers (2)

shashi singh
shashi singh

Reputation: 104

If you Know the Column name you can just use the Below.

"$row.MachineName" 

But if you don't Know column name or having some issue, still it can be accessed using an array of Select.

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

Upvotes: 0

Mene
Mene

Reputation: 3799

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.

Upvotes: 1

Related Questions