I am looking for a mySQL driver for nodejs that supports stored procedures. http://nodejsdb.org/db-mysql/ that I have been using gives the error
PROCEDURE can't return a result set in the given context
Felix Geisendörfer's node-mysql supports stored procedures, but you need to end your stored procedure by SELECT
ing a success/failure flag, then query it as you would a SELECT
query. Here's how the stored procedure might look:
DELIMITER //
DROP PROCEDURE IF EXISTS MyProcedure //
CREATE PROCEDURE MyProcedure(IN param1 VARCHAR/*, My, Parameters, ... */)
BEGIN
DECLARE EXIT HANDLER FOR NOT FOUND, SQLWARNING, SQLEXCEPTION SELECT 0 AS res;
# My Queries etc. ...
SELECT 1 AS res;
END //
DELIMITER ;
Your Node code would look something like this:
var mysql = require('mysql');
var client = mysql.createConnection({
host : '127.0.0.1',
user : 'username',
password: 'password'
});
client.query('USE mydatabase');
var myParams = "'param1', 'param2', ... ";
client.query("CALL MyProcedure(" + myParams + ")", function(err, results, fields) {
if (err || results[0].res === 0) {
throw new Error("My Error ... ");
} else {
// My Callback Stuff ...
}
});
it works in nodejs-mysql-native
stored procedure:
DELIMITER //
CREATE PROCEDURE test1p1()
BEGIN
SELECT 1+1;
END //
DELIMITER ;
node.js script:
mysql = require('mysql-native');
var db = mysql.createTCPClient();
db.auth('test', 'tester', ''); // db, user, password
db.query('call test.test1p1;').on('row', function(r) {
console.log(r);
}).on('end', function() {
console.log('OK!');
});
output:
{ '1+1': 2 }
OK!