本文介绍了计数INSERT/ON重复密钥更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个SQL查询:
INSERT INTO `Items`
(`id`,`parent_id`,`name`)
VALUES (123,321,null)
ON DUPLICATE KEY UPDATE
`id` = `id`, `parent_id` = VALUES(`parent_id`), `name` = VALUES(`name`);
我使用phpmyadmin运行查询,并返回正确的结果:
I run the query with phpmyadmin, and it returns me right results:
0 - No update
1 - Line inserted
2 - Line updated
但是当我通过java/jdbc运行查询时,查询返回1-1-2也就是说,如果根本没有插入/更新,则返回1.任何的想法?是一个错误还是我只是想念一些东西?
But when I run the query via java/jdbc, the query returns 1-1-2That is, 1 is returned when there's no insert/update at all.Any idea? Is it a bug or I just miss something?
更新:最新的连接器/j-v5.1.26
UPDATE: Latest connector/j - v5.1.26
UPDATE2:Java代码
UPDATE2: Java code
int ans1 = 0;
int ans2 = 0;
try {
java.sql.Connection c = DbConnector.getConn();
Statement s = c.createStatement();
ans1 = DbConnector.execute("INSERT INTO `Items` (`id`,`parent_id`,`name`) VALUES (123,9990635,null) ON DUPLICATE KEY UPDATE `id` = `id`, `parent_id` = VALUES(`parent_id`), `name` = VALUES(`name`);");
ans2 = DbConnector.execute("INSERT INTO `Items` (`id`,`parent_id`,`name`) VALUES (123,9990635,null) ON DUPLICATE KEY UPDATE `id` = `id`, `parent_id` = VALUES(`parent_id`), `name` = VALUES(`name`);");
s.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(ans1);
System.out.println(ans2);
推荐答案
根据在未进行任何更改的情况下,INSERT ON DUPLICATE返回1而不是0 您需要将属性"useAffectedRows"设置为"true".
According toINSERT ON DUPLICATE returns 1 instead of 0 when nothing is changedYou need to set property "useAffectedRows" to be "true".
这篇关于计数INSERT/ON重复密钥更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!