简体   繁体   English

错误:带有参数的“$1”postgresql 查询处或附近的语法错误

[英]ERROR: syntax error at or near "$1" postgresql query with params

I have a postgres query with params, in my Java code, am using stringBuilder to build the query and then createNativeQuery to create query and populating the params.我有一个带有参数的 postgres 查询,在我的 Java 代码中,我使用 stringBuilder 来构建查询,然后使用 createNativeQuery 来创建查询并填充参数。 However if I run the query in my gui, it gives me the result which is expected, but gives error when executing via code.但是,如果我在 gui 中运行查询,它会给我预期的结果,但在通过代码执行时会出错。 I'm a newbie on postgres and have seen some questions with the nearly same problems that I need to cast the params in the query, but I do have IN param inside my query which is list, I've tried some solutions but didn't worked.我是 postgres 的新手,看到了一些与我需要在查询中转换参数几乎相同的问题,但我的查询中有 IN 参数,即列表,我尝试了一些解决方案,但没有没用。

I' am getting error as :-> "error": "Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1" Position: 249我收到错误:-> "error": "Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: org.postgresql. util.PSQLException:错误:“$1”处或附近的语法错误位置:249

        StringBuilder sb = new StringBuilder();
        sb.append(" SELECT comments.comment, mapping.user_id, 
        mapping.comment_id, mapping.comment_state, 
        mapping.created_at  FROM comments ").append(" comment 
        JOIN ").
        append(" user_comment_mapping  mapping ON 
        mapping.comment_id = comment.id WHERE 
        mapping.user_id IN ? AND mapping.comment_state = ? 
        ");

        final Query query = em.createNativeQuery(sb.toString());
        query.setParameter("1", userIds);
        query.setParameter("2", 
        2);

        List<Object[]> list = query.getResultList();

Thanks!谢谢!

It seems you are mixing between parameter holder and name so.看来您在参数持有者和名称之间混用了。

If you want to use holder :如果你想使用 holder :

mapping.user_id IN ? AND mapping.comment_state = ? 

then然后

query.setParameter(1, userIds);
query.setParameter(2, 2);

If you want to use names :如果要使用名称:

mapping.user_id IN :ids AND mapping.comment_state = :stat


query.setParameter("ids", userIds);
query.setParameter("stat", 2);

暂无
暂无

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

相关问题 Postgresql:null 及其附近的语法错误 - Postgresql : syntax error at and near null 引起:org.postgresql.util.PSQLException:错误:“:”处或附近的语法错误 - Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near ":" org.postgresql.util.PSQLException:错误:在“with”处或附近出现语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near "with" org.postgresql.util.PSQLException:错误:“:”处或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “:” org.postgresql.util.PSQLException:错误:语法错误在“$ 1”或附近 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “$1” org.postgresql.util.PSQLException:错误:“ 7”或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “7” org.postgresql.util.PSQLException:错误:“)”处或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “)” org.postgresql.util.PSQLException:错误:“-”处或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “-” org.postgresql.util.PSQLException:错误:“(”或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “(” org.postgresql.util.PSQLException:错误:Java 中«,» 附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error near «,» in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM