node-mysql query attempts to replace my question marks with parameters against my will

StackOverflow https://stackoverflow.com/questions/9358451

  •  28-10-2019
  •  | 
  •  

Pergunta

I'm trying to run a simple query with node-mysql like this:

client.query("UPDATE mytable SET emote='wtf?' WHERE id=5");

And I get the error

Error: too few parameters given

Because it found a question mark and expects parameters. How the heck do I tell it to ignore the question mark!? Escaping it with \ doesn't work. Passing ['?'] as a parameter is completely out of the question - what if I don't know the amount of question marks the query contains?

Currently I'm using a workaround that counts the amount of question marks in the query and passes that amount of question marks as parameters. But what if I were to actually need the parameters for what they were meant to do? I shiver at the thought.

So to summarize - How do I tell client.query() to ignore question marks?

Outras dicas

Use this:

client.query('UPDATE mytable SET emote=? WHERE id=5', ['wtf?']);

Edit:

Passing ['?'] as a parameter is completely out of the question - what if I don't know the amount of question marks the query contains?

I skipped over this. Why would you not know how many question marks the query contains? Why would you have an unknown query with question marks?

When I try to recreate your scenario, the repeat use of single quotes causes a problem:

client.query('UPDATE mytable SET emote='wtf?' WHERE id=5');

Specifically I see "SyntaxError: Unexpected identifier" when I try to launch the Node instance.

However, I'm able to run an equivalent query just fine, when I switch the outer quotes to double quotes, like so:

mysql.query("SELECT * FROM stories WHERE title='Test?'");

For comparison, this is the parameterized version of my query:

mysql.query('SELECT * FROM stories WHERE title=?',[req.params.title]);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top