wingshauser wingshauser - 1 month ago 19
Node.js Question

Using a variable in a SQLITE Where clause in Node.js

I am new to SQL and am having a hard time writing an SQL query for my node application. I would like to use a variable in the WHERE statement, but it seems like there is more to it. This is what I have tried...

var itemID = 3;

var stmt = db.prepare("UPDATE tableName SET status = 'Out' WHERE id = itemID");
stmt.run();
stmt.finalize();


Any advice is appreciated. Thanks,
Alex

Answer

You have to move the variable out of the string and concatenate it

var stmt = db.prepare("UPDATE tableName SET status = 'Out' WHERE id = " + itemID);

With ES6 you can use string substitution. Note the backquotes around the string instead of double quotes

var stmt = db.prepare(`UPDATE tableName SET status = 'Out' WHERE id =  ${itemID}`);
Comments