简体   繁体   English

postgres准备声明中的几个关闭占位符

[英]Several close placeholders in postgres prepared statement

I'm using Spring JbdcTemplate with Postgres. 我正在使用Spring JbdcTemplate和Postgres。

Stuck with a problem cause of the Postrgres JDBC internal prepared statement implemetation 陷入Postrgres JDBC内部准备语句实现的问题原因

My query string is: 我的查询字符串是:

private static final String SELECT_ALL_PARTIALLY =
            "SELECT login, added FROM admin ORDER BY ? ? OFFSET ? LIMIT ?";

And I want to use it like: 我想用它像:

List matches = getJdbcTemplate().query(SELECT_ALL_PARTIALLY,
                (new Object[]{
                    column, // "login" or "added"
                    order,  // "asc" or "desc"
                    offset, 
                    limit
                }),
                new RowMapper() {
                    ...

The problem is with a ...ORDER BY ? ? OFFSET... 问题出在...ORDER BY ? ? OFFSET... ...ORDER BY ? ? OFFSET...

org.postgresql.util.PSQLException: ERROR: syntax error at or near "$2"
  Position: 61
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:273)
    at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:648) ...

How can I separate this two placeholders or something? 如何将这两个占位符或其他东西分开?

Postgres doesn't allow you to specify ordering direction as a parameter. Postgres不允许您将排序方向指定为参数。 You need to create different query strings for asc and desc order and choose one of them to execute. 您需要为ascdesc顺序创建不同的查询字符串,并选择其中一个执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM