你知道如何存储结果
sql.eachRow()
groovy sql 的列表?例如 def personList = [] ?
例子:
sql.eachRow('select lastname from users where id='active')
我想要的是存储结果,即。将姓氏添加到列表中,即 def personlist = []; 我知道我可以通过 namedQuery 轻松做到这一点,而且我已经做到了。但他们对此有一些潜在的原因。提前致谢。
def reqdColName = "lastname"
def query = "select $reqdColName from users where id='active'"
直接选项是使用rows () 方法。
def list= Sql.rows(query) //returns groovyrowresult as list
另一种选择是您可以改用executeQuery来获取查询的结果集,从而获得一个数组/列表。
def array = Sql.executeQuery(query).getArray(reqdColName)
//If you need as a list
def personList = Arrays.asList(array)
I know that the question has been asked a long time ago, but still, I feel the answer may be of help for someone out 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
def personList = sql.rows("select lastname from users where id='active'")*.lastname