Counting INSERT/ON DUPLICATE KEY UPDATE

旧城冷巷雨未停 提交于 2019-12-13 04:47:22

问题


I have this sql query:

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`);

I run the query with phpmyadmin, and it returns me right results:

0 - No update
1 - Line inserted
2 - Line updated

But when I run the query via java/jdbc, the query returns 1-1-2 That is, 1 is returned when there's no insert/update at all. Any idea? Is it a bug or I just miss something?

UPDATE: Latest connector/j - v5.1.26

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);

回答1:


According to INSERT ON DUPLICATE returns 1 instead of 0 when nothing is changed You need to set property "useAffectedRows" to be "true".



来源:https://stackoverflow.com/questions/19043803/counting-insert-on-duplicate-key-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!