Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a driver for mysql on nodejs that supports stored procedures?

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

like image 948
abinop Avatar asked May 11 '12 07:05

abinop


People also ask

Does MySQL support stored procedures?

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.

How to call a MySQL stored procedure from Node JS?

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.

Can I use MySQL with Node JS?

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.

What is node-MySQL and how to install it?

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:

How to make queries against managed databases using Node JS?

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:


2 Answers

Felix Geisendörfer's node-mysql supports stored procedures, but you need to end your stored procedure by SELECTing 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 ...

    }
});
like image 58
Loc Nguyen Avatar answered Oct 04 '22 17:10

Loc Nguyen


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

like image 32
Pushker Yadav Avatar answered Oct 04 '22 18:10

Pushker Yadav