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
MySQL supports stored routines (procedures and functions). A stored routine is a set of SQL statements that can be stored in the server. Once this has been done, clients don't need to keep reissuing the individual statements but can refer to the stored routine instead.
Calling MySQL Stored Procedures from Node.js. 1 Connect to the MySQL database server. 2 Call the stored procedure by executing the CALL spName statement. The spName is the name of the stored procedure. 3 Close the database connection.
MongoDB is currently the most popular on the market. However, you can also use MySQL with Node.js. Certainly, this tutorial was just a very basic example, but it can help to get an initial understanding for those who want to start a complete MySQL integration with Node.js.
Node-mysql is a client solution designed specifically for the implementation of the protocol associated with the well-known DBMS. The node-mysql module can be installed via the Node.js package manager. To install the module it will be sufficient to write the following command in the Terminal:
Once this is done, you can begin to make queries against the managed databases by sending them directly via Node.js; as a first step it will be necessary to call up the module just installed: Secondly, you will have to go to the connection phase with the Database engine and then to the selection of the database to manipulate:
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 ...
}
});
node-mysql driver work with stored procedure and its very simple just call your stored procedure with parameter.
CREATE PROCEDURE GetAllStudent(id int)
BEGIN
SELECT * FROM student where userid = id ;
END;
and in node just call
app.get('/sp', function (req, res, next) {
connection.connect();
connection.query('CALL GetAllStudent(?)',[req.body.id],function (err, rows, fields) {
if (err) {
res.status(400).send(err);
}
res.status(200).send(rows);
});
connection.end();
});
this way no need to worry of sql injection.
here is good tutorial on nodejs and mysql
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With