我试图插入一些播放器数据,如果它还不存在,但是我的语句每次都会创建一个新的行。这是我写的方法:
public void insertPlayerData(PlayerJoinEvent event) {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
String ign = player.getDisplayName();
String profile = "1";
try {
//Connect To Database
Connection con = getConnection();
//Prepare Query Statement
PreparedStatement posted = con.prepareStatement(
"INSERT INTO players (uuid, ign, profile)"
+ "VALUES ('"+uuid+"', '"+ign+"', '"+profile+"')"
+ "ON DUPLICATE KEY UPDATE profile = '"+profile+"'");
//Execute Statement
posted.executeUpdate();
} catch (Exception e) {
System.out.println(lb + "Error: " +e);
} finally {
System.out.println(lb + "Player Data Inserted");
}
}不幸的是,我的语句正在创建重复的行。我尝试过许多不同的方法,但都失败了。有什么想法吗?提前谢谢。
发布于 2015-12-28 19:58:08
您的查询显示重复的密钥更新配置文件=配置文件,这意味着在创建表时需要将配置文件作为唯一的键约束。
发布于 2015-12-28 21:31:43
1)在应该是唯一的列上创建唯一的键(似乎您想要唯一的“配置文件”)
2)使用:将忽略插入表..。它将尝试插入行,但如果唯一的键发现冲突-它只是忽略行和尝试下一个(如果它的大量插入或只是结束查询),没有任何错误。作为此查询的结果,将为您成功地为插入行编号。
如果您真的不需要更新行,请更快地忽略副本上的数据行。
https://stackoverflow.com/questions/34499633
复制相似问题