Matěj Zmítko Matěj Zmítko - 7 months ago 48
SQL Question

SQLite copy row and change two values in one query

I use SQLite and I need to copy one row and change 2 values preferably in one query.

Now I have a following code:

var timestamp = new Date().toString().replace(' GMT+0100 (CET)', '');
db.run("INSERT INTO scene (name,timestamp,user_id) SELECT name,timestamp,user_id FROM scene WHERE id = ?",req.param('scene'));
db.run("UPDATE scene SET timestamp=? WHERE id = ?",timestamp, req.param('scene');


It works fine, but I would like to update
timestamp
already in first query so I do not have to run another db statement.

Is there any way to to this?

Answer

I did not test it. But would this work?

var timestamp = new Date().toString().replace(' GMT+0100 (CET)', '');
db.run("INSERT INTO scene (name,timestamp,user_id) SELECT name,?,user_id FROM scene WHERE id = ?",timestamp,req.param('scene'));