SQLITE3 UPDATE CASE Node.js

时间:2015-06-12 11:52:09

标签: javascript node.js sqlite sql-update

我使用sqllite3 npm for node.js,我想做以下事情:

玩家可以玩游戏10次。 24小时后,计数器重置,他可以再玩10场比赛。我的想法是用更新案例来做。

 db.run("UPDATE user_sessions SET games_played = CASE WHEN((CAST(strftime('%s', CURRENT_TIMESTAMP) as integer) - CAST(strftime('%s', last_game) as integer))>86400 THEN "1" ELSE  ?1, last_game = CURRENT_TIMESTAMP WHERE ip_address = ?2 AND user_agent = ?3", {
        1:gamesPlayed,
        2:handshake.address,
        3:handshake.headers['user-agent']
    });

如果在上一场比赛之间和现在24小时不同(86400秒),他会更新并设置计数器1.

(CAST(strftime('%s', CURRENT_TIMESTAMP) as integer) - CAST(strftime('%s', last_game) as integer)

应该以秒为单位给出时差。这个对我有用。

完整的声明对我不起作用它说: 错误附近" THEN":语法错误

这种更新声明是否可行?如果是的话,我做错了什么?

我也在sqlite浏览器命令行中尝试了这个:

UPDATE user_sessions SET games_played = CASE WHEN(((CAST(strftime('%s', CURRENT_TIMESTAMP) as integer) - CAST(strftime('%s', last_game) as integer))>86400) THEN 1 ELSE  5, last_game = CURRENT_TIMESTAMP 

同样的错误。

1 个答案:

答案 0 :(得分:2)

您在第一个(之前有一个冗余的cast,并且缺少end表达式的case子句:

UPDATE user_sessions SET games_played = CASE WHEN(CAST(strftime('%s', CURRENT_TIMESTAMP) as integer) - CAST(strftime('%s', last_game) as integer))>86400 THEN "1" ELSE  ?1 END, last_game = CURRENT_TIMESTAMP WHERE ip_address = ?2 AND user_agent = ?3"