gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:07:47 25 4
gpt4 key购买 nike

我有一个函数可以将一条记录插入到 PostgreSQL 数据库中。我的问题是如何获取插入记录的 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());
});
}
});

最佳答案

在您的插入语句中添加一个 RETURNING 子句。参见 https://www.postgresql.org/docs/current/static/sql-insert.html

这是来自 vert.x 文档,参见 https://vertx.io/docs/vertx-mysql-postgresql-client/java/

Note about last inserted ids

When inserting new rows into a table, you might want to retrieve auto-incremented ids from the database. The JDBC API usually lets you retrieve the last inserted id from a connection. If you use MySQL, it will work the way it does like the JDBC API. In PostgreSQL you can add the "RETURNING" clause to get the latest inserted ids. Use one of the query methods to get access to the returned columns.

关于java - 如何使用postgres sql客户端获取vertx中插入记录的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51984186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com