Store sql.eachRow () from groovy sql to list

You know how to save the result from

sql.eachRow() 

from groovy sql to list? for example def personList = []?

Example:

  sql.eachRow('select lastname from users where id='active') 

I want to save the result, i.e. lastnames to list, i.e. def personlist = []; I know that I can easily do this with namedQuery, and I already did it. But this is one of the main reasons for this. Thanks in advance.

+4
source share
3 answers
 def reqdColName = "lastname" def query = "select $reqdColName from users where id='active'" 

The direct option is to use the rows () method.

 def list= Sql.rows(query) //returns groovyrowresult as list 

Another option is that you could use executeQuery instead to get a list of query results and thereby get an array / list in the subject.

 def array = Sql.executeQuery(query).getArray(reqdColName) //If you need as a list def personList = Arrays.asList(array) 
+3
source

I know that the question has been asked for a long time, but, nevertheless, I feel that the answer can help someone there.

  def personlist = [] sql = Sql.newInstance(url, username, password, driver) query = "select $reqdColName from users where id='active'" sql.eachRow(query) { row-> personlist << row.toString() } sql.close() personlist.each{println it} // you can also return the list 
+1
source
 def lastnames = sql.rows("select lastname from users where id='active'")*.lastname 
0
source

All Articles