简体   繁体   English

如何使用postgres sql客户端获取vertx中插入记录的id

[英]How to get the id of an inserted record in vertx using postgres sql client

I have a function that insert a single record into a PostgreSQL database. 我有一个函数将一条记录插入PostgreSQL数据库。 My question is how do i get the ID of the inserted record since the ID is auto generated This is the code 我的问题是如何获取插入记录的ID,因为ID是自动生成的这是代码

postgreSQLClient.getConnection(ar -> {
        if(ar.failed())
              LOGGER.error("saving message error:", ar.cause());
        else{
           SQLConnection conn = ar.result();
           conn.queryWithParams(insertStr.toString(), sqlParam, rs -> {
               conn.close();
               Constant.LOGGER.info("Save ID: " +rs.result());
           });
        }
    });

Add a RETURNING clause to your insert statement. 在insert语句中添加RETURNING子句。 See https://www.postgresql.org/docs/current/static/sql-insert.html 请参阅https://www.postgresql.org/docs/current/static/sql-insert.html

This is from the vert.x documentation, see https://vertx.io/docs/vertx-mysql-postgresql-client/java/ 这是来自vert.x文档,请参阅https://vertx.io/docs/vertx-mysql-postgresql-client/java/

Note about last inserted ids 关于最后插入的ID的注意事项

When inserting new rows into a table, you might want to retrieve auto-incremented ids from the database. 将新行插入表时,您可能希望从数据库中检索自动递增的ID。 The JDBC API usually lets you retrieve the last inserted id from a connection. JDBC API通常允许您从连接中检索最后插入的id。 If you use MySQL, it will work the way it does like the JDBC API. 如果您使用MySQL,它将像JDBC API一样工作。 In PostgreSQL you can add the "RETURNING" clause to get the latest inserted ids. 在PostgreSQL中,您可以添加“RETURNING”子句以获取最新插入的ID。 Use one of the query methods to get access to the returned columns. 使用其中一种查询方法来访问返回的列。

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

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