Similar to connection.query()
.
connection.execute('select 1 + ? + ? as result', [5, 6], (err, rows) => {
});
connection.unprepare('select 1 + ? + ? as result');
Note that connection.execute()
will cache the prepared statement for better performance, remove the cache with connection.unprepare()
when you're done.
Manually prepared statements doesn't comes with LRU cache and SHOULD be closed using statement.close()
instead of connection.unprepare()
.
connection.prepare('select ? + ? as tests', (err, statement) => {
statement.execute([1, 2], (err, rows, columns) => {
});
statement.close();
});
Note that you should not use statement after connection reset (changeUser()
or disconnect). Statement scope is connection, you need to prepare statement for each new connection in order to use it.
maxPreparedStatements
: We keep the cached statements in a lru-cache. Default size is 16000
but you can use this option to override it. Any statements that are dropped from cache will be closed
.
The bind parameter values passed to execute
are serialized JS -> MySQL as:
null
-> NULL
number
-> DOUBLE
boolean
-> TINY
(0 for false, 1 for true)object
-> depending on prototype:
Date
-> DATETIME
JSON
like object - JSON
Buffer
-> VAR_STRING
VAR_STRING
Passing in undefined
or a function
will result in an error.
MySQL2 provides execute
helper which will prepare and query the statement. You can also manually prepare / unprepare statement with prepare
/ unprepare
methods.
connection.execute(
'select ?+1 as qqq, ? as rrr, ? as yyy',
[1, null, 3],
(err, rows, fields) => {
console.log(err, rows, fields);
connection.execute(
'select ?+1 as qqq, ? as rrr, ? as yyy',
[3, null, 3],
(err, rows, fields) => {
console.log(err, rows, fields);
connection.unprepare('select ?+1 as qqq, ? as rrr, ? as yyy');
connection.execute(
'select ?+1 as qqq, ? as rrr, ? as yyy',
[3, null, 3],
(err, rows, fields) => {
console.log(err, rows, fields);
}
);
}
);
}
);
Examples
For Prepared Statements examples, please see here.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4