SQLite3 UPDATE CASE Node.js

I am using sqllite3 npm for node.js and I want to do the following:

The player can play the game 10 times. 24 hours later, the counter is reset and it can play 10 games again. My thought was doing this with the update case.

 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']
    });

      

It updates and sets the counter to 1 if there is 24 hours (86400 seconds) between the last game and now.

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

      

Should give the time difference in seconds. I like.

The full statement doesn't work for me, it says: error near "THEN": syntax error

Is such an update approval possible? If so, what am I doing wrong?

I also tried this in the sqlite browser command line:

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 

      

The same mistakes.

+3


source to share


1 answer


You are redundant (

to the first cast

and missing an end

expression clause 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"

      

+2


source







All Articles