gpt4 book ai didi

sql - grails-使用GroovyRowResult的列名打印每行的值

转载 作者:行者123 更新时间:2023-12-02 14:25:01 24 4
gpt4 key购买 nike

我想让用户启动sql查询,然后在grails View 页面中查看结果。

我的QueryController.groovy

def query(){
def headers = null; //["id","name"]
Sql sql = new Sql(dataSource)
def rowResults = sql.rows(params.query) //GroovyRowResult
rowResults.eachWithIndex { row, index->
if (headers == null) {
headers = row.keySet()
}
}
[headers : headers, resultList: rowResults, total : rowResults.size() ]

}

在grails查看页面( query.gsp)中,
<table class="table table-striped">
<thead>
<tr>
<g:each in="${headers}" var="header">
<g:sortableColumn property="header" title="${header}" />
<th></th>
</g:each>
</tr>
</thead>
<tbody>
<g:each in="${resultList}" var="row">
<tr>
<g:each status="counter" in="${row}" var="val">
<td>${val}</td>
</g:each>
</tr>
</g:each>

</tbody>
</table>

View 中的 <td>${val}</td>部分未按预期工作,因为它以 id=1而不是 1的形式给出结果。我只想在其中显示值。

虽然可能是一个较小的问题,但需要修复它。

谢谢。

最佳答案

尝试通过访问每个 map 上的值来访问值:

<table class="table table-striped" border="1">
<thead>
<tr>
<g:each in="${headers}" var="header">
<g:sortableColumn property="header" title="${header}" />
</g:each>
</tr>

</thead>
<tbody>
<g:each in="${resultList}" var="row">
<tr>
<g:each status="counter" in="${row}" var="val">
<td>${val.value}</td>
</g:each>
</tr>
</g:each>

</tbody>
</table>

/

另外,在查询操作中,您可以直接从 map 获取标题:
 def query(){
def headers = null; //["id","name"]
Sql sql = new Sql(dataSource)
def rowResults = sql.rows("select * from Message;") //GroovyRowResult

// rowResults is a list, you can access it by its index
headers = rowResults[0].keySet()

FYI, what you are giving your users is very powerful and they can run any sort of query against you database even to drop your tables.

关于sql - grails-使用GroovyRowResult的列名打印每行的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18417088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com