对于INSERT ON ON DUPLICATE KEY UPDATE,Java MySQL executeUpdate()返回什么? [英] What does the Java MySQL executeUpdate() returns for INSERT ON ON DUPLICATE KEY UPDATE?
问题描述
我花了大约3个小时在互联网上查看,我仍然找不到这个问题的答案。
I have spent about 3 hours checking on the internet and I still can't find the answer to this question.
返回:(1)SQL数据操作语言
(DML)语句的行数或(2)0表示不返回任何内容的SQL语句
Returns: either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing
那么它的意思是:
- 插入1行没有重复 - > 1
- 插入3行没有重复 - > 3
- 插入包含所有重复项的3行,没有更新值 - > 0
- 插入3行,其中2个不重复,1个重复更新值 - > 3
- 插入3行,其中包含2个no-du plicates和1个重复不更新值 - > 2
- Insert 1 row without duplicates -> 1
- Insert 3 rows without duplicates -> 3
- Insert 3 rows with all duplicates and none updating a value -> 0
- Insert 3 rows with 2 no-duplicates and 1 duplicate that updates a value -> 3
- Insert 3 rows with 2 no-duplicates and 1 duplicate that doesn't update a value -> 2
或者至少这是什么我从文档中了解到。
Or at least this is what I understand from the documentation.
然而,阅读其他问题 here 和 here 让我感到困惑,因为它没有描述这种行为,相反,它描述了MySQL 文档所说的内容
However, reading other questions here and here on StackOverflow got me confused again since it doesn't describe this behavior, instead it describes what the MySQL documentation says
使用ON DUPLICATE KEY UPDATE,如果
将行插入为新行,则每行的受影响行值为1 ,如果现有行更新,则为2;如果存在,则为
0将行设置为其当前值。
With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values.
如果以前的行为是使用连接器和Java的独占性,这仍然有意义后来通过直接的MySQL操作,但正如我之前所说,我发现答案表明它使用的是第一个或第二个相互矛盾。
This would still make sense if the former behavior was exclusive to Java using the connector and the later through direct MySQL manipulation, but as I said before, I have found answers that say it uses either the first or the second contradicting each other.
这对我很重要因为在我的代码中,我执行了一个插入更新的许多条目,并通过比较返回数字检查它是否成功执行,我的数组修改计数。
This is important for me because on my code, I perform an insert with on update of many entries and check if it was performed successfully by comparing the return number, with my array to modify count.
推荐答案
使用ON DUPLICATE KEY UPDATE,如果将行作为新行插入,则每行的受影响行值为1,如果更新现有行,则为2。
With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if the row is inserted as a new row, and 2 if an existing row is updated.
这篇关于对于INSERT ON ON DUPLICATE KEY UPDATE,Java MySQL executeUpdate()返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!