Postgres JDBC driver: PSQLException: syntax error at or near RETURNING

前端 未结 3 2023
无人共我
无人共我 2021-02-13 14:56

For some reason the JDBC PostgreSQL driver is adding: RETURNING * to the end of select statements. Why?

Code:

protected         


        
相关标签:
3条回答
  • 2021-02-13 15:23

    This looks like a problem with sql2o. The comments on the bug report say:

    Whe using PostgreSQL, all SELECT statements will fail with message: org.postgresql.util.PSQLException: ERROR: syntax error at or near "RETURNING"

    Seems to be related to this issue

    This has been fixed with version 1.1.2.

    The fix requires the QuirkMode enum flag to be set to PostgreSQL when creating a new instance of sql2o. It changes default behaviour of queries to NOT fetch generated keys by default. When it is needed to fetch generated keys, the returnGeneratedKeys parameter in the generateQuery method should be set.

    Since Sql2o 1.6.0, include the sql2o-postgres dependency and use new PostgresQuirks() instead of QuirksMode.

    0 讨论(0)
  • 2021-02-13 15:32

    In my case occurred when u tried to insert empty list of objects.

    0 讨论(0)
  • 2021-02-13 15:39

    The easiest way I've done this was adding ";--" at the end of sql code:

    String sql = "INSERT INTO testTable(var1, var2) values ("1","2"), ("1","2") RETURNING id;--";
    
    PreparedStatement ps = getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
    ps.executeUpdate();
    ResultSet rs = ps.getGeneratedKeys();
    
    0 讨论(0)
提交回复
热议问题