gpt4 book ai didi

带有JDBC的多列的Postgresql array_agg

转载 作者:行者123 更新时间:2023-11-29 13:00:27 32 4
gpt4 key购买 nike

我正在尝试连接一个表,该表可能具有多个给定 ID 的条目,并将与该 ID 对应的行聚合到一个数组中。这在 SQL 查询中看起来如下:

SELECT * from data
LEFT JOIN (select id, array_agg(row(foo, bar)) AS foo_bar_data from foo_bar_table group by id) AS temp using(id)

这按预期工作,但我无法在 JDBC 中读取结果。

ResultSet rs = st.executeQuery(...)
Array a = rs.getArray("foo_bar_data")
// Now I want to iterate over the array, reading the values foo and bar of each item.

到目前为止,我的努力总是以 Method org.postgresql.jdbc4.Jdbc4Array.getArrayImpl(long,int,Map) is not yet implemented. 异常结束。我如何遍历 a,检索值 foobar

编辑:我可能还应该提一下,foobar 的类型不同。

最佳答案

Postgres JDBC 驱动程序不支持除基本类型(数字、日期/时间戳、字符串)以外的任何 JDBC 数组。您可以调用 array_agg 两次并在每行上获得两个数组:

    try (Connection db = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "postgres");
ResultSet rs = db.createStatement().executeQuery("select array_agg(i), array_agg(s) from (select 1 i, 'a' s union select 2 i, 'b' s) t")) {
rs.next();
System.out.println(Arrays.toString((Object[]) rs.getArray(1).getArray()));
System.out.println(Arrays.toString((Object[]) rs.getArray(2).getArray()));
}

关于带有JDBC的多列的Postgresql array_agg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32016829/

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