Groovy Sql 获取所有列

发布于 2024-12-27 13:43:39 字数 1476 浏览 2 评论 0原文

我正在使用 Groovy Sql 来获取结果。这是我的 Linux 机器的输出。

%isql -U abc -P abc -S support
1> sp_configure 'number of open partitions'
2> go
 Parameter Name                 Default     Memory Used Config Value    Run Value    Unit                 Type
 ------------------------------ ----------- ----------- ------------  ------------ -------------------- ----------
 number of open partitions              500        5201         5000          5000 number               dynamic

(1 row affected)
(return status = 0)
1>

我正在使用常规代码

def sql = Sql.newInstance("jdbc:abc:sybase://harley:6011;DatabaseName=support;",dbuname,dbpassword,Driver)
sql.eachRow("sp_configure 'number of open partitions'"){ row ->
        println row.run_value
    }

,但它给了我

Exception in thread "main" java.sql.SQLSyntaxErrorException: [abc][Sybase JDBC Driver]Invalid column name: run_value

所以它说它无法获取指定的列,有没有办法可以获取结果并显示?

更新

我使用了以下代码

sql.eachRow("sp_configure 'number of open partitions'"){ row ->
            println row
    }

,它给了我

[Parameter Name:number of open partitions     , Default:        500, Memory Used:       5201, Config Value:        5000, Run Value:        5000, Unit:number              , Type:dynamic   ]

如何获得运行值? (因为它里面有一个空格)

row.Run Value 肯定不会工作

I am using Groovy Sql to fetch results. This is the output from my Linux box.

%isql -U abc -P abc -S support
1> sp_configure 'number of open partitions'
2> go
 Parameter Name                 Default     Memory Used Config Value    Run Value    Unit                 Type
 ------------------------------ ----------- ----------- ------------  ------------ -------------------- ----------
 number of open partitions              500        5201         5000          5000 number               dynamic

(1 row affected)
(return status = 0)
1>

I am using groovy code

def sql = Sql.newInstance("jdbc:abc:sybase://harley:6011;DatabaseName=support;",dbuname,dbpassword,Driver)
sql.eachRow("sp_configure 'number of open partitions'"){ row ->
        println row.run_value
    }

but it gives me

Exception in thread "main" java.sql.SQLSyntaxErrorException: [abc][Sybase JDBC Driver]Invalid column name: run_value

So it says it cannot get the speciied columns, is there a way it can fetch the result and display?

Update

I used the below code

sql.eachRow("sp_configure 'number of open partitions'"){ row ->
            println row
    }

and it gives me

[Parameter Name:number of open partitions     , Default:        500, Memory Used:       5201, Config Value:        5000, Run Value:        5000, Unit:number              , Type:dynamic   ]

How can I get Run Value? (since it has a space in it)

row.Run Value will not work for sure

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霞映澄塘 2025-01-03 13:43:39

这有效吗?查询元数据中的列名并通过索引获取值:

sql.eachRow("sp_configure 'number of open partitions'"){ row ->
  (1..row.getMetaData().columnCount).each {
    println "${row.metaData().getColumnName( it )} = ${row[ it ]}"
  }
}

或者直接获取列

row.'Run Value'

Does this work? Querying the meta-data for the column name and getting the value by index:

sql.eachRow("sp_configure 'number of open partitions'"){ row ->
  (1..row.getMetaData().columnCount).each {
    println "${row.metaData().getColumnName( it )} = ${row[ it ]}"
  }
}

Or, to get the column directly

row.'Run Value'
多彩岁月 2025-01-03 13:43:39

除了 @tim_yates 的回答之外,我想指出 rowtoRowResult() 方法。它允许简单地获取一行作为 Map。如果我们只需要列名或表宽度,则无需调用 getMetadata()

sql.eachRow("sp_configure 'number of open partitions'"){ row ->
  row.toRowResult().each(){ colName, value ->
    println "${colName} = ${value}"
  }
}

这段代码的作用完全相同,但看起来更清晰

Additioannly to @tim_yates's answer I'd like to note that row has toRowResult() method. It allows to get a row simply as a Map. No need to call getMetadata() if we need only column names or table width.

sql.eachRow("sp_configure 'number of open partitions'"){ row ->
  row.toRowResult().each(){ colName, value ->
    println "${colName} = ${value}"
  }
}

This code doing absolutely the same but looks much more clear

娇纵 2025-01-03 13:43:39

另一种选择是 getAt(int)。在您的情况下,这将返回“运行值”:

row.getAt(-3)

Another alternative is getAt(int). In your case, this will return the 'Run Value':

row.getAt(-3)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文