문제

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);
도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top