【问题标题】:Exception sending three arrays with unnest(array[someArray]) in JDBC在 JDBC 中使用 unnest(array[someArray]) 发送三个数组的异常
【发布时间】:2019-10-16 05:33:32
【问题描述】:

我无法将 3 个数组发送到 SQL 语句中,它会引发异常

select t.ttt, t.created_date, concat_ws('-',lp.mmm, lp.ccc, lp.eee ) from tasks t
join positions p on t.id = p.task_id
left join lte_position lp on p.id = lp.id
where t.ttt in (:identities) and (lp.mmm, lp.ccc, lp.eee) in
((select mmm,ccc,eee from ((select unnest(array[:mmm]) as mmm, unnest(array[:ccc]) as ccc, unnest(array[:eee]) as eee)) as temp))
order by t.ttt DESC

我想在mmmccceee 中发送 3 个数组。当我在 Postgres 控制台上尝试时,它工作正常,但是当我在 Java 代码中尝试时,抛出异常

org.postgresql.util.PSQLException: ERROR: cannot cast type record to integer
Position: 474

【问题讨论】:

    标签: java postgresql jdbc jdbctemplate


    【解决方案1】:

    按照你写的方式,PostgreSQL认为你的整个参数字符串是一个整数。

    而不是

    unnest(array[:mmm])
    

    使用

    unnest(CAST(:mmm AS integer[])
    

    但是参数必须看起来像

    {1,23,456}
    

    包括大括号。这是PostgreSQL中数组的字符串表示。

    补充观察:你为什么使用:mmm? JDBC 参数必须写为?

    【讨论】:

    • {1,23,456} 如何使用 jdbctemplate 发送此参数?
    • 不知道,没听说过 jdbctemplate。我以为问题是关于 JDBC 的。
    • 用于 jaca Spring jdbc 的 NamedParameterJdbcTemplate
    猜你喜欢
    • 2015-08-24
    • 2016-03-06
    • 2013-01-15
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 2019-12-27
    相关资源
    最近更新 更多