0

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...});
1
  • When you only use a single replacement character ? then you don't have to use an array. Use one or the other, it's up to you, there's no difference. Commented Jan 3, 2021 at 10:28

1 Answer 1

1

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])
  }
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.