nodejs / community-committee

The Node.js Community Committee (aka CommComm)
MIT License
263 stars 70 forks source link

node.js sqlite placeholder #535

Closed raffaeleiavazzo85 closed 5 years ago

raffaeleiavazzo85 commented 5 years ago

Can you tell me if these considerations are correct? Thanks

I can use placeholders in a node.js-sqlite query in these 3 ways:

1)Directly in the function arguments:

      db.run("UPDATE tbl SET name = ? WHERE id = ?", "bar", 2);

      /*first ? is a placeholder and it is replaced with "bar" value 
      before the query is executed

      second ? is a placeholder and it is replaced with 2 value before 
      the query is executed*/

..............................................................................

2)As an array:

      db.run("UPDATE tbl SET name = ? WHERE id = ?", [ "bar", 2 ]);

     /*Second argument passed to db.run function is a parameters array.

     first ? is a placeholder and it is replaced with first array 
     item("bar") before the query is executed

     second ? is a placeholder and it is replaced with second array 
     item(2) before the query is executed*/

............................................................................. 3)As an object with named parameters:

      db.run("UPDATE tbl SET name = $name WHERE id = $id", {
          $id: 2,
          $name: "bar"
      });

//In this case, the second argument of the db.run function is an object.

    /*$name placeholder in the UPDATE query, is replaced with the 
    $name key 
    value("bar") of the object before the query is executed

    $id placeholder in the UPDATE query, is replaced with the $id key 
    value(2) of the object before the query is executed

    placeholder and object key names must be the same.*/

..............................................................................

All correct?

WaleedAshraf commented 5 years ago

Hi @Raffaele85,

I would suggest you ask your question in Nodejs Slack: https://www.nodeslackers.com/

codeekage commented 5 years ago

Will be closing this. :) please use the slack.