Can different placeholder types be mixed in Node-SQLite?


Can different placeholder types be mixed in Node-SQLite?


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:"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:"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:"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.

1 Like

Just for fun, what would happen if we had more than one value left?
Let’s say I write this:"UPDATE tbl SET title = $title name = ?5 WHERE id = ? AND serial = ?",
    1: 2,
    5: "bar",
    $title: 'Mrs.',
    3: 1001,
    2: 1234

Would SQLite choose the id value as the first remaining value and the serial value as the second remaining? Would the third remaining value be selected in priority because it is numbered as 2?

I realize this is bad code since it is not cleaned and optimized, but I am just curious about how the query would react in such a scenario.