插入HSQLDB的更新

时间:2015-04-17 13:05:33

标签: java sql hsqldb

我正在尝试重写使用MySQL数据库的代码来使用HSQLDB。我有一个INSERT INTO ... ON DUPLICATE KEY UPDATE查询。 HSQLDB是否有此方法的替代方法。

代码:

PreparedStatement ps = conn.prepareStatement("INSERT INTO user_stats "
    + "(user, balance, kills, level)"
    + " VALUES(?, ?, ?, ?) ON DUPLICATE KEY "
    + "UPDATE balance=VALUES(balance), kills=VALUES(kills), level=VALUES(level)");

ps.setString(1, playername);
ps.setDouble(2, PluginInterract.getESSBalance(playername));
ps.setInt(3, PluginInterract.getKills(playername));
ps.setDouble(4, PluginInterract.getPluginLevel(playername));

2 个答案:

答案 0 :(得分:2)

从版本2。3。3(2015年6月)开始,HSQLDB在MYS兼容模式下支持INSERT INTO ... ON DUPLICATE KEY UPDATE ...

参考:http://hsqldb.org/doc/guide/guide.html#coc_compatibility_mysql

但是版本2.3.3不支持MySQL的Values(COLUMN_NAME)方法。

答案 1 :(得分:1)

不符合the documentation。但是,MERGE语句可能会执行您想要的操作。以下是基于MERGE的文档的未经测试的示例:

MERGE INTO user_stats USING (VALUES(?, ?, ?, ?))
    AS vals(user, balance, kills, level) ON user_stats.user = vals.user
    WHEN MATCHED THEN UPDATE SET user_stats.balance = vals.balance, user_stats.kills = vals.kills, user_stats.level = vals.level
    WHEN NOT MATCHED THEN INSERT VALUES vals.user, vals.balance, vals.kills, vals.level