gpt4 book ai didi

java - 如果不存在则插入mysql条目,如果存在则更新java

转载 作者:行者123 更新时间:2023-11-29 13:24:44 25 4
gpt4 key购买 nike

我想插入一个条目(如果它不存在),否则更新该条目,我无法使用 ON DUPLICATE KEY UPDATE,我对语法感到困惑。所以我尝试做这样的事情:

    final String QUERY = "REPLACE INTO skills SET VALUES (" + insert(player) + ") WHERE playername = '" + player.getUsername() + "'";
statement.execute(QUERY);

statement.close();
connection.close();
}

private static String insert(Player player) {
String stringToReturn = "'" + player.getUsername() + "',";
for (int index = 0; index < 25; index++) {
stringToReturn += player.getSkills().getLevels()[index] + "," + ((int) player.getSkills().getXp()[index]) + ",";
}
stringToReturn = stringToReturn.substring(0, stringToReturn.length() - 1);
return stringToReturn;
}

但是这是不正确的语法,所以我想知道如何做到这一点?

玩家名是主键

最佳答案

我认为使 ON DUPLICATE KEY UPDATE 为您工作的正确语法是:

"INSERT INTO skills (playerName, otherColumn) 
VALUES ('" + player.getUsername() + "', '" + insert(player) +"')
ON DUPLICATE KEY UPDATE otherColumn = VALUES(otherColumn)";

关于java - 如果不存在则插入mysql条目,如果存在则更新java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20302669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com