NODEJS - mysqlPool.getConnection - 在MYSQL中调用存储过程 - 返回值

时间:2015-10-15 05:17:12

标签: mysql node.js

我在mysql中有一个存储过程正常工作。我试图用NODEJS来称呼它。如何在nodejs中获取存储过程的out值?

  mysqlPool.getConnection(function(error,conn){
        conn.query(" SET @p1   =   '"  + P_MEMBER_ID           + "'  ; " +
            " SET @p2      =       '"  + P_MEMBER_ID_TEST      + "'  ; " +
            " SET @p3      =       @insertResults'"  + "'  ; " +
            "CALL PROC_MEMBER_INSERT(" +
                " @p1   ,  "  +
                " @p2   ,  "  +
                " @p3 )", function(error,results)
            {
                if (error)
                {
                    console.error(error);
                    throw error;
                }else{
                    console.log(results);
                     res.status(200).jsonp({insertResults:test[2][0]});       
                };
            });
            conn.release();
        });





      MYSQL stored proc example. 
      ..
     CREATE DEFINER=`phil`@`%` PROCEDURE `PROC_MEMBER_INSERT`(
       in P_MEMBER_ID         INT,
       in P_MEMBER_ID_TEST    INT,
      out RESULT_MESSAGE          varchar(120)
      )
      BEGIN
       .....

由于 菲尔

========================

我仍有问题......

   var connection = mysql.createConnection({
        host: '11.11.11.11',
        user: 'phil',
        password: 'password',
        port: 3306,
        database: 'testdb',
        connectionLimit : 100,
        multipleStatements: true
    });



    var query = connection.query(" SET @p1      =       '"  + P_MEMBER_ID                    + "'  ; " +
                                 " SET @p2      =       '"  + P_MEMBER_ID_TEST               + "'  ; " +
                                 " SET @p3      =            @insertResults "                + "   ; " +
         "CALL PROC_MEMBER_INSERT(" +
        " @p1   ,  "  +
        " @p2   ,  "  +
        " @p3 ); select @insertResults;");
    query
        .on('error', function(err) {
            // Handle error, an 'end' event will be emitted after this as well
            console.log('error  ============= ', err);
        })
        .on('fields', function(fields) {
            // the field packets for the rows to follow

            console.log('fields ============= ', fields);

            //res.status(200).jsonp({insertResults:fields});       
        })
        .on('result', function(row) {
            // Pausing the connnection is useful if your processing involves I/O
            // connection.pause();

            console.log('results ============= ', row);

            //res.status(200).jsonp({insertResults:row});      

            //processRow(row, function() {
            //    connection.resume();
            //});
        })
        .on('end', function() {
            // all rows have been received
        });


  MYSQL stored proc example. 
  ..
 CREATE DEFINER=`phil`@`%` PROCEDURE `PROC_MEMBER_INSERT`(
   in P_MEMBER_ID         INT,
   in P_MEMBER_ID_TEST    INT,
  out RESULT_MESSAGE          varchar(120)
  )
  BEGIN
   ....

输出看起来是空的。

没有记录被输入。

   ========= output ==============

           api: 2 -- end PROC_MEMBER_INSERT
            api: 2 P_MEMBER_ID ---------->  5
            api: 2 P_MEMBER_ID_TEST ----->  1195
            results =============  { fieldCount: 0,
              affectedRows: 0,
              insertId: 0,
              serverStatus: 10,
              warningCount: 0,
              message: '',
              protocol41: true,
              changedRows: 0 }
            results =============  { fieldCount: 0,
              affectedRows: 0,
              insertId: 0,
              serverStatus: 10,
              warningCount: 0,
              message: '',
              protocol41: true,
              changedRows: 0 }
            results =============  { fieldCount: 0,
              affectedRows: 0,
              insertId: 0,
              serverStatus: 10,
              warningCount: 0,
              message: '',
              protocol41: true,
              changedRows: 0 }
            results =============  { fieldCount: 0,
              affectedRows: 1,
              insertId: 0,
              serverStatus: 10,
              warningCount: 0,
              message: '',
              protocol41: true,
              changedRows: 0 }
            fields =============  [ { catalog: 'def',
                db: '',
                table: '',
                orgTable: '',
                name: '@RESULT_MESSAGE',
                orgName: '',
                charsetNr: 63,
                length: 16777216,
                type: 251,
                flags: 128,
                decimals: 31,
                default: undefined,
                zeroFill: false,
                protocol41: true } ]
            results =============  { '@insertResults': null }

=============

我添加了更多测试内容,现在看到它正在下降到.on('结果并且始终为null。

          results =============  { fieldCount: 0,
            affectedRows: 0,
            insertId: 0,
            serverStatus: 10,
            warningCount: 0,
            message: '',
            protocol41: true,
            changedRows: 0 }
          row user not found
          results =============  { fieldCount: 0,
            affectedRows: 0,
            insertId: 0,
            serverStatus: 10,
            warningCount: 0,
            message: '',
            protocol41: true,
            changedRows: 0 }
          row user not found
          results =============  { fieldCount: 0,
            affectedRows: 0,
            insertId: 0,
            serverStatus: 10,
            warningCount: 0,
            message: '',
            protocol41: true,
            changedRows: 0 }
          row user not found
          results =============  { fieldCount: 0,
            affectedRows: 1,
            insertId: 0,
            serverStatus: 2,
            warningCount: 0,
            message: '',
            protocol41: true,
            changedRows: 0 }


      .on('error', function(err) {
            // Handle error, an 'end' event will be emitted after this as well
            console.log('error  ============= ', err);
            return;
        })
        .on('fields', function(fields) {
            // the field packets for the rows to follow
            console.log('fields ============= ', fields);



            if (fields && fields.length)
                console.log('fields user found :)');
            else
                console.log('fields user not found');




            //res.status(200).jsonp({winkResults:fields}); 
        })
        .on('result', function(row) {
            // Pausing the connnection is useful if your processing involves I/O
            // connection.pause();

            console.log('results ============= ', row);

            if (row && row.length)
                console.log('row user found :)');
            else
                console.log('row user not found');


            //res.status(200).jsonp({winkResults:row});      

            //processRow(row, function() {
            //    connection.resume();
            //});
        })
        .on('end', function() {


            // all rows have been received
        });

3 个答案:

答案 0 :(得分:0)

select @p3;

之后的con.query语句中添加CALL PROC_MEMBER_INSERT

希望这有帮助!

答案 1 :(得分:0)

您应该创建一个启用多语句的连接;

var connection = mysql.createConnection({multipleStatements: true});

执行查询时,可以通过字段和结果访问out值,如下所示; var query = connection.query(' SET @p1 = '" + P_MEMBER_ID + "' ; " + " SET @p2 = '" + P_MEMBER_ID_TEST + "' ; " + " SET @p3 = @insertResults'" + "' ; " + "CALL PROC_MEMBER_INSERT(" + " @p1 , " + " @p2 , " + " @p3 ');)"; query .on('fields', function(fields, index) { // the fields for the result rows that follow }) .on('result', function(row, index) { // index refers to the statement this result belongs to (starts at 0) }); 字段数组应该保留您的过程的值。

我无法对其进行测试,但如果字段数组中不存在输出参数,那么 - 如果您的逻辑与此符合,则可以添加另一个{{1 }}语句到您的过程调用,它将访问输出,如下所示。

SELECT

参考文献: https://github.com/felixge/node-mysql#multiple-statement-queries http://dev.mysql.com/doc/refman/5.5/en/stored-routines-syntax.html

答案 2 :(得分:0)

通过简单的双重选择解决了我的问题。

选择'插入'作为双重结果;

选择'失败'作为双重结果;

我没有使用或弄清楚如何获取out参数。下次我会花更多的时间在这个问题上。

由于 菲尔