P H Kaznowski
P H Kaznowski

Reputation: 301

How does one collect sql rows in Groovy

I am trying to make a stream collect on my SQL rowset within a groovy script, however the output is an empty array. Calling print within the closure works.

Any ideas on how I can processes all the rows without having to consume them in the first closure and without referencing out-of-scope variables?

import groovy.sql.Sql

def sql = Sql.newInstance("jdbc:mysql://localhost", "login", "password", "com.mysql.jdbc.Driver")

// Display databases
println "Databases: "
println sql.eachRow("SHOW databases") {row -> "${row[0]}"}.collect()

Any help much appreciated, thanks!

Upvotes: 1

Views: 1277

Answers (1)

Opal
Opal

Reputation: 84784

Have a look at the docs. None of eachRow methods returns a collection.

Maybe try:

println sql.rows("SHOW databases").collect { row -> "${row[0]}" }

Upvotes: 1

Related Questions