Skip to content
Advertisement

use quotes in sql query node.js

I have my query like so:

but when I insert the query into my database (I am storing the query for analytical purposes), it fails because of the quotes.

how can I fix this? I tried converting thequery to toString(), but that was a useless attempt. Any other ideas?

edit:

error i am recieving is:

Advertisement

Answer

This is a great example of how using parameterized queries is better than using string-concatenation to format SQL queries. You can use parameter placeholders and then you never have to worry about the possible literal quote characters in your string variables.

You don’t even need the single-quotes around the ? placeholders. In fact, you must not quote the placeholders, or else it will try to insert a literal string “?” instead of treating it as a parameter.

Using query parameters makes your code easier to read and easier to write, without getting eyestrain trying to balance all those quotes-within-quotes.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement