Fez Vrasta
Fez Vrasta

Reputation: 14825

node-mysql2 and prepared statements

I'd like to know what could I use as alternative for PHP PDO in Node.js

I've found node-mysql2, they talk about prepared statements but I can't find a way to cache the statement to reuse it.

With PHP I do:

$sth = $pdo->prepare('SELECT 1+? AS test1');
$sth->execute( Array(10) );
$sth->execute( Array(20) );

And with this I don't prepare the query each time, I juse use the prepared one changing the variable value.

With node-mysql2 instead I do:

connection.execute('SELECT 1+? AS test1', [10], function(err, rows) {
  //
});

But I can't find a way to take advantages by the prepared statement feature.

Upvotes: 2

Views: 2936

Answers (1)

Noah Goodrich
Noah Goodrich

Reputation: 25263

Referencing this thread near the end you can see this answer to your question:

Sorry, I was completely wrong about node-mysql2. It does indeed prepare once. The API led me to believe that it didn't have a separate prepare and execute, but I looked at the code and found that it does have a cache that reuses the prepared statement if you use the same query text.

I was a bit nasty when I posted that comment. I was frustrated. Sorry about that.

Upvotes: 4

Related Questions