简体   繁体   中英

Update on Insert HSQLDB

I am trying to rewrite code, that was using MySQL database, to use HSQLDB. I have an INSERT INTO ... ON DUPLICATE KEY UPDATE query. Is there an alternative of this method for HSQLDB.

Code:

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

From version 2.3.3 (June 2015) HSQLDB supports INSERT INTO ... ON DUPLICATE KEY UPDATE ... in MYS compatibility mode.

Refer: http://hsqldb.org/doc/guide/guide.html#coc_compatibility_mysql

But version 2.3.3 doesn't support Values(COLUMN_NAME) method of MySQL.

Not according to the documentation . The MERGE statement might do what you want, however. Here is an untested example based on the documentation for 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM