JDBC - INSERT 并返回生成的 id,或者如果 DUPLICATE KEY 返回旧 id
标题已经说得很清楚了... 我希望能够将一个项目添加到数据库中,如果已经有一个项目具有匹配的唯一列,则返回该项目的 id...
的解决方案最接近
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
下面的表达式与我找到 使用statement. generatedKeys() 获得新的id(插入新行时)。 如果出现重复,“id”会出现在statement. generatedKeys() 中吗?如果没有,我怎样才能获取它?
更新:使用存储过程,以下正是我所需要的:
INSERT INTO applications (path, displayName) VALUES (?, ?)
ON DUPLICATE KEY UPDATE
id = LAST_INSERT_ID(id), path = ?, displayName = ?;
SELECT LAST_INSERT_ID() INTO ?;
它适用于我正在开发的不同项目,该项目在.NET中使用MySQL连接器,但不幸的是存储过程不适合我这个Java项目。
有没有其他方法可以在同一个网络行程中发送 2 个语句?
The title is pretty clear...
I want to be able to add an item to the database, and in case there's already an item with a matching unique column, return that item's id...
The expression below is as close as I found to a solution
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
I was able to fetch the new id (when inserting a new row) using statement.generatedKeys().
will "id" appear in statement.generatedKeys() in case of duplicate? if not, how can I fetch it?
UPDATE : using stored procedure, the following is exactly what I need:
INSERT INTO applications (path, displayName) VALUES (?, ?)
ON DUPLICATE KEY UPDATE
id = LAST_INSERT_ID(id), path = ?, displayName = ?;
SELECT LAST_INSERT_ID() INTO ?;
It works on different project i'm working on which uses MySQL Connector in .NET, but unfortunately Stored Procedure is not an option for me in this Java project.
Is there any other way I can send 2 statements in the same network trip?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这看起来合法
,但这看起来很疯狂:
您会更新现有记录的 ID 字段吗?引用此记录的那些记录会发生什么情况?
不管怎样,猜测在正常更新的情况下(第一个)
statement. generatedKeys()
不会返回已经存在的 ID。在这种情况下,您可能需要发出额外的 select 语句(在 where 子句中使用唯一键列:
select id from table where a=1 and b=2
)来查找记录。或者,您也可以编写一个存储过程,在两种情况下插入/更新都会返回 ID。
This seems legit
but this seems crazy:
Would you update the ID field of an existing record? What will happen to those records referencing to this one?
Anyway, guess in case of a normal update (the forst one)
statement.generatedKeys()
wont return the already existing ID.In this case you'll probably have to issue an additional select statement (with the unique key columns in the where clause:
select id from table where a=1 and b=2
) to find out ID of the record.Or alternatively you could write a stored procedure which does the insert/update returns the ID in both case.
所以我的要求和OP一样。 Mysql 处理 INSERT...ON DUPLICATE KEY UPDATE 的方式很愚蠢,但不幸的是我们必须忍受它。当您在 Mysql 5.5 或更高版本上使用 ON DUPLICATE 时,如果插入没有重复,则 JDBC 将返回计数 1 和新插入行的 ID。如果所采取的操作是由于重复而进行的更新,则 JDBC 将返回计数 2 以及原始行的 ID 和新生成的 ID,即使新 ID 从未实际插入到表中。
您可以通过调用PreparedStatement.getGenerateKeys()来获取正确的密钥。第一个键几乎总是您感兴趣的键。因此,对于上面的示例:
您可以通过调用以下方式获取插入或更新的 ID:
So I just had the same requirement as the OP. The way Mysql handles INSERT...ON DUPLICATE KEY UPDATE is just asinine, but unfortunately we have to live with it. When you use ON DUPLICATE on Mysql 5.5 or above, if the insert happens without duplicate, JDBC returns count of 1 and the ID of the newly inserted row. If the action taken is an update due to duplicate, JDBC returns count of 2 and both the ID of the original row AND the newly generated ID, even though the new ID is never actually inserted into the table.
You can get the correct key by calling PreparedStatement.getGeneratedKeys(). The first key is pretty much always the one you are interested in. So with the above example:
You can get the inserted or updated ID by calling:
如果您的目标是防止重复记录,则可以使用主键/UNIQUE 约束< /a>.然后,您可以在代码中采用以下路径之一:
If your goal is to prevent duplicate records, you can set up your table with a primary key/UNIQUE constraint. You could then take one of the following paths in your code: