ahmadalibin
ahmadalibin

Reputation: 141

Difference between these two sql queries node js

Is there a difference between using the query with [] and without? I'm supposed to choose the best one that does the same thing as this SELECT * FROM books WHERE name LIKE 'Pride and Prejudice'; but when I use both queries in node.js, they give the exact same output?

db.query("SELECT * FROM books WHERE name LIKE ?;",
"Pride and Prejudice",
(err, result) => { // the rest of the code...});
db.query("SELECT * FROM books WHERE name LIKE ?;",
["Pride and Prejudice"],
(err, result) => { // the rest of the code...});

Upvotes: 0

Views: 112

Answers (1)

Anatoly
Anatoly

Reputation: 22793

I didn't find examples without passing an array to parameters, so I'd recommend always to use an array to pass parameters even if you have just one.

From the official documentation (the only one parameter also passed in an array):

const query = {
  // give the query a unique name
  name: 'fetch-user',
  text: 'SELECT * FROM user WHERE id = $1',
  values: [1],
}
// callback
client.query(query, (err, res) => {
  if (err) {
    console.log(err.stack)
  } else {
    console.log(res.rows[0])
  }
})

Upvotes: 1

Related Questions