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

后端 未结 5 1675
孤独总比滥情好
孤独总比滥情好 2020-12-09 19:40

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 r

5条回答
  •  失恋的感觉
    2020-12-09 20:04

    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 ...
    
        }
    });
    

提交回复
热议问题