Can different placeholder types be mixed in Node-SQLite?


#1

Question

Can different placeholder types be mixed in Node-SQLite?

Answer

It is possible, but it is also not recommended because besides it being messy, it can lead to errors, for example, let’s compare the following:

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

Here’s an update query that changes two values, by using the ? placeholder we can go through the values in sequential order from the array, that is to say, the name will be the first value, and id the second, always. It is efficient and clean. In this other case:

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

we assigned specific placeholders, and thus, no matter the order, they will be assigned correspondingly, but what if it is mixed:

db.run("UPDATE tbl SET title=$title name = ?5 WHERE id = ?", {
          1: 2,
          5: "bar"
         $title: 'Mrs.'
      });

Here three things are happening:
• we have a specific placeholder $title that will be sought for no matter which position it is.
• there is a ?5 placeholder, which works the same way as $title, it searches for the key 5 and assigns its value.
• and we have ? that, in this case, will take the first value left.

It can get confusing, and thus not recommendable and not a good practice, but it is possible.